Junjie
2026-01-16 5bee5c93889da3a08178b1345c3a24ebde41c989
#通知上报异步化
1个文件已添加
2个文件已修改
173 ■■■■■ 已修改文件
src/main/java/com/zy/asrs/service/NotifyAsyncService.java 94 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/asrs/task/NotifyScheduler.java 77 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application.yml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/asrs/service/NotifyAsyncService.java
New file
@@ -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;
/**
 * 异步通知服务
 * 用于异步发送HTTP通知请求,避免阻塞定时器线程
 */
@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);
        }
    }
}
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);
        }
    }
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: