From 51c5843cb7d3c66d1223f0603463bb2e9ba9f456 Mon Sep 17 00:00:00 2001 From: wr <1754607820@qq.com> Date: Mon, 8 Jan 2024 16:40:08 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0=E5=9B=BD=E7=BD=91=E4=B8=8A?= =?UTF-8?q?=E9=80=81=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=EF=BC=8C=E9=87=8D?= =?UTF-8?q?=E8=AF=95=E6=9C=BA=E5=88=B6=202.=E6=8B=86=E5=88=86=E5=9B=BD?= =?UTF-8?q?=E7=BD=91=E4=B8=8A=E9=80=81=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/controller/TimersController.java | 2 +- .../njcn/system/service/ITimersService.java | 2 +- .../service/impl/TimersServiceImpl.java | 28 +++++++++++++++-- .../timer/impl/HutoolTimerExeServiceImpl.java | 29 ++++++++++++++++-- .../tasks/MonitorSendOtherUserTaskRunner.java | 30 +++++++++++++++++++ .../MonitorSendPhotovoltaicTaskRunner.java | 30 +++++++++++++++++++ .../timer/tasks/MonitorSendTaskRunner.java | 15 ---------- .../MonitorSendTractionStationTaskRunner.java | 30 +++++++++++++++++++ .../src/main/resources/bootstrap.yml | 5 ++++ 9 files changed, 149 insertions(+), 22 deletions(-) create mode 100644 pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendOtherUserTaskRunner.java create mode 100644 pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendPhotovoltaicTaskRunner.java create mode 100644 pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendTractionStationTaskRunner.java diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/controller/TimersController.java b/pqs-system/system-boot/src/main/java/com/njcn/system/controller/TimersController.java index a9666a92e..c68544d41 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/controller/TimersController.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/controller/TimersController.java @@ -172,7 +172,7 @@ public class TimersController extends BaseController { @OperateInfo(info = LogEnum.SYSTEM_COMMON) @GetMapping("/run") @ApiOperation("执行一次定时任务") - public HttpResult run(String id) { + public HttpResult run(String id) throws InterruptedException { String methodDescribe = getMethodDescribe("run"); timersService.run(id); return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe); diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/service/ITimersService.java b/pqs-system/system-boot/src/main/java/com/njcn/system/service/ITimersService.java index 7ee158c9b..feb719f56 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/service/ITimersService.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/service/ITimersService.java @@ -32,5 +32,5 @@ public interface ITimersService extends IService { void stop(String id); - void run(String id); + void run(String id) throws InterruptedException; } diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/TimersServiceImpl.java b/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/TimersServiceImpl.java index db449f6a7..3f7ae1b6c 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/TimersServiceImpl.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/service/impl/TimersServiceImpl.java @@ -25,6 +25,7 @@ import com.njcn.system.timer.TimerExeService; import com.njcn.system.timer.TimerTaskRunner; import com.njcn.web.factory.PageFactory; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -48,6 +49,11 @@ public class TimersServiceImpl extends ServiceImpl impleme private final TimerExeService timerExeService; + @Value("${runTake.maxRetryCount}") + private Integer maxRetryCount; + @Value("${runTake.delayTime}") + private Integer delayTime; + @Override public Page listTimer(TimersParam queryParam) { QueryWrapper queryWrapper = new QueryWrapper<>(); @@ -130,7 +136,7 @@ public class TimersServiceImpl extends ServiceImpl impleme } @Override - public void run(String id) { + public void run(String id) throws InterruptedException { Timers sysTimers = this.queryTimers(id); String actionClass = sysTimers.getActionClass(); TimerTaskRunner timerTaskRunner; @@ -139,7 +145,7 @@ public class TimersServiceImpl extends ServiceImpl impleme } catch (ClassNotFoundException e) { throw new BusinessException(SystemResponseEnum.TIMER_NO_CLASS); } - timerTaskRunner.action(); + runTaskWithRetryAndDelay(timerTaskRunner); } /** @@ -156,4 +162,22 @@ public class TimersServiceImpl extends ServiceImpl impleme return timers; } + public void runTaskWithRetryAndDelay(TimerTaskRunner timerTaskRunner) throws InterruptedException { + int retryCount = 0; // 重试次数计数器 + while (retryCount < maxRetryCount) { + try { + timerTaskRunner.action(); + return; // 任务执行成功,跳出循环 + } catch (Exception e) { + // 处理异常 + retryCount++; // 增加重试次数 + Thread.sleep(delayTime); // 等待一段时间 + } + } + if (retryCount >= maxRetryCount) { + // 达到最大重试次数,终止任务 + return; + } + } + } diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/timer/impl/HutoolTimerExeServiceImpl.java b/pqs-system/system-boot/src/main/java/com/njcn/system/timer/impl/HutoolTimerExeServiceImpl.java index 5dff9dcbc..0d5885015 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/timer/impl/HutoolTimerExeServiceImpl.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/timer/impl/HutoolTimerExeServiceImpl.java @@ -6,11 +6,11 @@ import cn.hutool.cron.CronUtil; import cn.hutool.cron.task.Task; import cn.hutool.extra.spring.SpringUtil; import cn.hutool.log.Log; -import cn.hutool.setting.Setting; import com.njcn.common.pojo.exception.BusinessException; import com.njcn.system.enums.SystemResponseEnum; import com.njcn.system.timer.TimerExeService; import com.njcn.system.timer.TimerTaskRunner; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; /** @@ -22,7 +22,10 @@ import org.springframework.stereotype.Service; public class HutoolTimerExeServiceImpl implements TimerExeService { private static final Log log = Log.get(); - + @Value("${runTake.maxRetryCount}") + private Integer maxRetryCount; + @Value("${runTake.delayTime}") + private Integer delayTime; @Override public void startTimer(String taskId, String cron, String className) { @@ -41,9 +44,11 @@ public class HutoolTimerExeServiceImpl implements TimerExeService { Task task = () -> { try { TimerTaskRunner timerTaskRunner = (TimerTaskRunner) SpringUtil.getBean(Class.forName(className)); - timerTaskRunner.action(); + runTaskWithRetryAndDelay(timerTaskRunner); } catch (ClassNotFoundException e) { log.error(">>> 任务执行异常:{}", e.getMessage()); + } catch (InterruptedException e) { + log.error(">>> 运行任务执行异常:{}", e.getMessage()); } }; @@ -56,4 +61,22 @@ public class HutoolTimerExeServiceImpl implements TimerExeService { CronUtil.remove(taskId); } + public void runTaskWithRetryAndDelay(TimerTaskRunner timerTaskRunner) throws InterruptedException { + int retryCount = 0; // 重试次数计数器 + while (retryCount < maxRetryCount) { + try { + System.out.println("重试机制:"+(retryCount+1)); + timerTaskRunner.action(); + return; // 任务执行成功,跳出循环 + } catch (Exception e) { + // 处理异常 + retryCount++; // 增加重试次数 + Thread.sleep(delayTime); // 等待一段时间 + } + } + if (retryCount >= maxRetryCount) { + // 达到最大重试次数,终止任务 + return; + } + } } diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendOtherUserTaskRunner.java b/pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendOtherUserTaskRunner.java new file mode 100644 index 000000000..b211f34be --- /dev/null +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendOtherUserTaskRunner.java @@ -0,0 +1,30 @@ +package com.njcn.system.timer.tasks; + +import com.njcn.device.pms.api.MonitorSendClient; +import com.njcn.device.pms.pojo.param.MonitorParam; +import com.njcn.system.service.SysDicTreePOService; +import com.njcn.system.timer.TimerTaskRunner; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + + +/** + * + */ +@Component +@RequiredArgsConstructor +public class MonitorSendOtherUserTaskRunner implements TimerTaskRunner { + + private final MonitorSendClient monitorSendClient; + private final SysDicTreePOService sysDicTreePOService; + + + @Override + public void action() { + //干扰用户 + MonitorParam.Info param4 = new MonitorParam.Info(); + param4.setObjType(sysDicTreePOService.queryByCode("2300").getId()); + param4.setFiy(true); + monitorSendClient.windSend(param4); + } +} diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendPhotovoltaicTaskRunner.java b/pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendPhotovoltaicTaskRunner.java new file mode 100644 index 000000000..02e56471e --- /dev/null +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendPhotovoltaicTaskRunner.java @@ -0,0 +1,30 @@ +package com.njcn.system.timer.tasks; + +import com.njcn.device.pms.api.MonitorSendClient; +import com.njcn.device.pms.pojo.param.MonitorParam; +import com.njcn.system.service.SysDicTreePOService; +import com.njcn.system.timer.TimerTaskRunner; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + + +/** + * + */ +@Component +@RequiredArgsConstructor +public class MonitorSendPhotovoltaicTaskRunner implements TimerTaskRunner { + + private final MonitorSendClient monitorSendClient; + private final SysDicTreePOService sysDicTreePOService; + + + @Override + public void action() { + //光伏电站 + MonitorParam.Info param1 = new MonitorParam.Info(); + param1.setObjType(sysDicTreePOService.queryByCode("1402").getId()); + param1.setFiy(true); + monitorSendClient.windSend(param1); + } +} diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendTaskRunner.java b/pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendTaskRunner.java index 9022489d2..ff0a82927 100644 --- a/pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendTaskRunner.java +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendTaskRunner.java @@ -26,20 +26,5 @@ public class MonitorSendTaskRunner implements TimerTaskRunner { param.setObjType(sysDicTreePOService.queryByCode("1401").getId()); param.setFiy(true); monitorSendClient.windSend(param); - //光伏电站 - MonitorParam.Info param1 = new MonitorParam.Info(); - param1.setObjType(sysDicTreePOService.queryByCode("1402").getId()); - param1.setFiy(true); - monitorSendClient.windSend(param1); - //牵引站 - MonitorParam.Info param3 = new MonitorParam.Info(); - param3.setObjType(sysDicTreePOService.queryByCode("1300").getId()); - param3.setFiy(true); - monitorSendClient.windSend(param3); - //干扰用户 - MonitorParam.Info param4 = new MonitorParam.Info(); - param4.setObjType(sysDicTreePOService.queryByCode("2300").getId()); - param4.setFiy(true); - monitorSendClient.windSend(param4); } } diff --git a/pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendTractionStationTaskRunner.java b/pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendTractionStationTaskRunner.java new file mode 100644 index 000000000..2f7ce6ada --- /dev/null +++ b/pqs-system/system-boot/src/main/java/com/njcn/system/timer/tasks/MonitorSendTractionStationTaskRunner.java @@ -0,0 +1,30 @@ +package com.njcn.system.timer.tasks; + +import com.njcn.device.pms.api.MonitorSendClient; +import com.njcn.device.pms.pojo.param.MonitorParam; +import com.njcn.system.service.SysDicTreePOService; +import com.njcn.system.timer.TimerTaskRunner; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + + +/** + * + */ +@Component +@RequiredArgsConstructor +public class MonitorSendTractionStationTaskRunner implements TimerTaskRunner { + + private final MonitorSendClient monitorSendClient; + private final SysDicTreePOService sysDicTreePOService; + + + @Override + public void action() { + //牵引站 + MonitorParam.Info param3 = new MonitorParam.Info(); + param3.setObjType(sysDicTreePOService.queryByCode("1300").getId()); + param3.setFiy(true); + monitorSendClient.windSend(param3); + } +} diff --git a/pqs-system/system-boot/src/main/resources/bootstrap.yml b/pqs-system/system-boot/src/main/resources/bootstrap.yml index 75d2889aa..c9ff7293a 100644 --- a/pqs-system/system-boot/src/main/resources/bootstrap.yml +++ b/pqs-system/system-boot/src/main/resources/bootstrap.yml @@ -53,6 +53,11 @@ mybatis-plus: #别名扫描 type-aliases-package: com.njcn.system.pojo +runTake: + #最大重试次数 + maxRetryCount: 5 + #等待时间(毫秒) + delayTime: 5000 mqtt: client-id: @artifactId@${random.value}