From 5bee5c93889da3a08178b1345c3a24ebde41c989 Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期五, 16 一月 2026 08:36:15 +0800
Subject: [PATCH] #通知上报异步化

---
 src/main/java/com/zy/asrs/service/NotifyAsyncService.java |   94 +++++++++++++++++++++++++++++++
 src/main/java/com/zy/asrs/task/NotifyScheduler.java       |   77 +++++--------------------
 src/main/resources/application.yml                        |    2 
 3 files changed, 111 insertions(+), 62 deletions(-)

diff --git a/src/main/java/com/zy/asrs/service/NotifyAsyncService.java b/src/main/java/com/zy/asrs/service/NotifyAsyncService.java
new file mode 100644
index 0000000..e2ac52c
--- /dev/null
+++ b/src/main/java/com/zy/asrs/service/NotifyAsyncService.java
@@ -0,0 +1,94 @@
+package com.zy.asrs.service;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.zy.asrs.domain.NotifyDto;
+import com.zy.asrs.entity.HttpRequestLog;
+import com.zy.common.utils.HttpHandler;
+import com.zy.common.utils.RedisUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
+
+import java.util.Date;
+
+/**
+ * 寮傛閫氱煡鏈嶅姟
+ * 鐢ㄤ簬寮傛鍙戦�丠TTP閫氱煡璇锋眰锛岄伩鍏嶉樆濉炲畾鏃跺櫒绾跨▼
+ */
+@Service
+@Slf4j
+public class NotifyAsyncService {
+
+    @Autowired
+    private RedisUtil redisUtil;
+    @Autowired
+    private HttpRequestLogService httpRequestLogService;
+
+    /**
+     * 寮傛鍙戦�侀�氱煡
+     * 
+     * @param notifyUri     閫氱煡URI
+     * @param notifyUriPath 閫氱煡璺緞
+     * @param key           Redis閿�
+     * @param notifyDto     閫氱煡鏁版嵁
+     */
+    @Async
+    public void sendNotifyAsync(String notifyUri, String notifyUriPath, String key, NotifyDto notifyDto) {
+        HttpRequestLog httpRequestLog = new HttpRequestLog();
+        httpRequestLog.setName(notifyUri + notifyUriPath);
+        httpRequestLog.setRequest(JSON.toJSONString(notifyDto));
+        httpRequestLog.setCreateTime(new Date());
+
+        boolean success = false;
+        try {
+            // 瑙﹀彂閫氱煡
+            String response = new HttpHandler.Builder()
+                    .setUri(notifyUri)
+                    .setPath(notifyUriPath)
+                    .setJson(JSON.toJSONString(notifyDto))
+                    .build()
+                    .doPost();
+            httpRequestLog.setResponse(response);
+
+            JSONObject jsonObject = JSON.parseObject(response);
+            Integer code = jsonObject.getInteger("code");
+            if (code == 200) {
+                // 閫氱煡鎴愬姛
+                redisUtil.del(key);
+                success = true;
+            }
+        } catch (Exception e) {
+            log.error("寮傛閫氱煡澶辫触, key={}", key, e);
+        } finally {
+            // 淇濆瓨璁板綍
+            httpRequestLogService.insert(httpRequestLog);
+        }
+
+        if (!success) {
+            // 閫氱煡澶辫触锛屾洿鏂伴噸璇曟鏁�
+            handleNotifyFailure(key, notifyDto);
+        }
+    }
+
+    /**
+     * 澶勭悊閫氱煡澶辫触鐨勬儏鍐�
+     */
+    private void handleNotifyFailure(String key, NotifyDto notifyDto) {
+        try {
+            int times = notifyDto.getRetryTimes() + 1;
+            if (times >= notifyDto.getFailTimes()) {
+                // 瓒呰繃娆℃暟
+                redisUtil.del(key);
+                return;
+            }
+
+            notifyDto.setLastRetryTime(System.currentTimeMillis());
+            notifyDto.setRetryTimes(times);
+            redisUtil.set(key, notifyDto);
+        } catch (Exception e) {
+            log.error("澶勭悊閫氱煡澶辫触閫昏緫寮傚父, key={}", key, e);
+        }
+    }
+}
diff --git a/src/main/java/com/zy/asrs/task/NotifyScheduler.java b/src/main/java/com/zy/asrs/task/NotifyScheduler.java
index 8e40b08..202a5ad 100644
--- a/src/main/java/com/zy/asrs/task/NotifyScheduler.java
+++ b/src/main/java/com/zy/asrs/task/NotifyScheduler.java
@@ -1,15 +1,11 @@
 package com.zy.asrs.task;
 
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.mapper.EntityWrapper;
 import com.zy.asrs.domain.NotifyDto;
 import com.zy.asrs.entity.DeviceConfig;
-import com.zy.asrs.entity.HttpRequestLog;
 import com.zy.asrs.service.DeviceConfigService;
-import com.zy.asrs.service.HttpRequestLogService;
+import com.zy.asrs.service.NotifyAsyncService;
 import com.zy.asrs.utils.NotifyUtils;
-import com.zy.common.utils.HttpHandler;
 import com.zy.common.utils.RedisUtil;
 import com.zy.core.enums.SlaveType;
 import com.zy.system.entity.Config;
@@ -19,7 +15,6 @@
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
-import java.util.Date;
 import java.util.List;
 
 @Component
@@ -33,12 +28,12 @@
     @Autowired
     private ConfigService configService;
     @Autowired
-    private HttpRequestLogService httpRequestLogService;
-    @Autowired
     private DeviceConfigService deviceConfigService;
+    @Autowired
+    private NotifyAsyncService notifyAsyncService;
 
     @Scheduled(cron = "0/3 * * * * ? ")
-    public synchronized void notifyShuttle(){
+    public synchronized void notifyShuttle() {
         List<DeviceConfig> deviceList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
                 .eq("device_type", String.valueOf(SlaveType.Crn)));
         for (DeviceConfig device : deviceList) {
@@ -47,7 +42,7 @@
     }
 
     @Scheduled(cron = "0/3 * * * * ? ")
-    public synchronized void notifyForkLift(){
+    public synchronized void notifyForkLift() {
         List<DeviceConfig> deviceList = deviceConfigService.selectList(new EntityWrapper<DeviceConfig>()
                 .eq("device_type", String.valueOf(SlaveType.Rgv)));
         for (DeviceConfig device : deviceList) {
@@ -56,23 +51,23 @@
     }
 
     @Scheduled(cron = "0/3 * * * * ? ")
-    public synchronized void notifyTask(){
+    public synchronized void notifyTask() {
         notifyMsg("task", 1);
     }
 
     @Scheduled(cron = "0/3 * * * * ? ")
-    public synchronized void notifyCrn(){
-        notifyMsg("crn", 1);
+    public synchronized void notifyCrn() {
+        notifyMsg(String.valueOf(SlaveType.Crn), 1);
     }
 
     @Scheduled(cron = "0/3 * * * * ? ")
-    public synchronized void notifyDualCrn(){
-        notifyMsg("dualCrn", 1);
+    public synchronized void notifyDualCrn() {
+        notifyMsg(String.valueOf(SlaveType.DualCrn), 1);
     }
 
     private synchronized void notifyMsg(String notifyType, Integer device) {
         Config notifyEnableConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyEnable"));
-        if(notifyEnableConfig == null){
+        if (notifyEnableConfig == null) {
             return;
         }
         String notifyEnable = notifyEnableConfig.getValue();
@@ -81,19 +76,19 @@
         }
 
         Config notifyUriConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyUri"));
-        if(notifyUriConfig == null){
+        if (notifyUriConfig == null) {
             return;
         }
         String notifyUri = notifyUriConfig.getValue();
 
         Config notifyUriPathConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyUriPath"));
-        if(notifyUriPathConfig == null){
+        if (notifyUriPathConfig == null) {
             return;
         }
         String notifyUriPath = notifyUriPathConfig.getValue();
 
         List<String> keys = notifyUtils.takeKeys(notifyType, device);
-        if(keys == null){
+        if (keys == null) {
             return;
         }
 
@@ -112,48 +107,8 @@
                 continue;
             }
 
-            HttpRequestLog httpRequestLog = new HttpRequestLog();
-            httpRequestLog.setName(notifyUri + notifyUriPath);
-            httpRequestLog.setRequest(JSON.toJSONString(notifyDto));
-            httpRequestLog.setCreateTime(new Date());
-
-            try {
-                //瑙﹀彂閫氱煡
-                String response = new HttpHandler.Builder()
-                        .setUri(notifyUri)
-                        .setPath(notifyUriPath)
-                        .setJson(JSON.toJSONString(notifyDto))
-                        .build()
-                        .doPost();
-                httpRequestLog.setResponse(response);
-
-                JSONObject jsonObject = JSON.parseObject(response);
-                Integer code = jsonObject.getInteger("code");
-                if(code == 200){
-                    //閫氱煡鎴愬姛
-                    redisUtil.del(key);
-
-                    continue;
-                }
-            }catch (Exception e){
-                e.printStackTrace();
-            }finally {
-                //淇濆瓨璁板綍
-                httpRequestLogService.insert(httpRequestLog);
-            }
-
-            //閫氱煡澶辫触
-            int times = notifyDto.getRetryTimes() + 1;
-            if (times >= notifyDto.getFailTimes()) {
-                //瓒呰繃娆℃暟
-                redisUtil.del(key);
-                continue;
-            }
-
-            notifyDto.setLastRetryTime(System.currentTimeMillis());
-            notifyDto.setRetryTimes(times);
-            redisUtil.set(key, notifyDto);
-            continue;
+            // 寮傛鍙戦�侀�氱煡锛岄伩鍏嶉樆濉炲畾鏃跺櫒绾跨▼
+            notifyAsyncService.sendNotifyAsync(notifyUri, notifyUriPath, key, notifyDto);
         }
     }
 
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 1ce512f..e98d76c 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -1,6 +1,6 @@
 # 绯荤粺鐗堟湰淇℃伅
 app:
-  version: 1.0.2
+  version: 1.0.2.1
   version-type: dev  # stable 鎴� dev
 
 server:

--
Gitblit v1.9.1