1.增加国网上送定时任务,重试机制

2.拆分国网上送定时任务接口
This commit is contained in:
wr
2024-01-08 16:40:08 +08:00
parent bd89190ddc
commit 51c5843cb7
9 changed files with 149 additions and 22 deletions

View File

@@ -172,7 +172,7 @@ public class TimersController extends BaseController {
@OperateInfo(info = LogEnum.SYSTEM_COMMON)
@GetMapping("/run")
@ApiOperation("执行一次定时任务")
public HttpResult<Object> run(String id) {
public HttpResult<Object> run(String id) throws InterruptedException {
String methodDescribe = getMethodDescribe("run");
timersService.run(id);
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);

View File

@@ -32,5 +32,5 @@ public interface ITimersService extends IService<Timers> {
void stop(String id);
void run(String id);
void run(String id) throws InterruptedException;
}

View File

@@ -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<TimersMapper, Timers> impleme
private final TimerExeService timerExeService;
@Value("${runTake.maxRetryCount}")
private Integer maxRetryCount;
@Value("${runTake.delayTime}")
private Integer delayTime;
@Override
public Page<TimersVO> listTimer(TimersParam queryParam) {
QueryWrapper<TimersVO> queryWrapper = new QueryWrapper<>();
@@ -130,7 +136,7 @@ public class TimersServiceImpl extends ServiceImpl<TimersMapper, Timers> 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<TimersMapper, Timers> impleme
} catch (ClassNotFoundException e) {
throw new BusinessException(SystemResponseEnum.TIMER_NO_CLASS);
}
timerTaskRunner.action();
runTaskWithRetryAndDelay(timerTaskRunner);
}
/**
@@ -156,4 +162,22 @@ public class TimersServiceImpl extends ServiceImpl<TimersMapper, Timers> 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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -53,6 +53,11 @@ mybatis-plus:
#别名扫描
type-aliases-package: com.njcn.system.pojo
runTake:
#最大重试次数
maxRetryCount: 5
#等待时间(毫秒)
delayTime: 5000
mqtt:
client-id: @artifactId@${random.value}