| src/main/java/com/zy/asrs/service/NotifyAsyncService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| src/main/java/com/zy/asrs/task/NotifyScheduler.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| src/main/resources/application.yml | ●●●●● 补丁 | 查看 | 原始文档 | 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,9 +28,9 @@ @Autowired private ConfigService configService; @Autowired private HttpRequestLogService httpRequestLogService; @Autowired private DeviceConfigService deviceConfigService; @Autowired private NotifyAsyncService notifyAsyncService; @Scheduled(cron = "0/3 * * * * ? ") public synchronized void notifyShuttle(){ @@ -62,12 +57,12 @@ @Scheduled(cron = "0/3 * * * * ? ") public synchronized void notifyCrn(){ notifyMsg("crn", 1); notifyMsg(String.valueOf(SlaveType.Crn), 1); } @Scheduled(cron = "0/3 * * * * ? ") public synchronized void notifyDualCrn(){ notifyMsg("dualCrn", 1); notifyMsg(String.valueOf(SlaveType.DualCrn), 1); } private synchronized void notifyMsg(String notifyType, Integer device) { @@ -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: