mirror of https://gitee.com/stylefeng/roses
【7.3.2】【timer】修复定时任务启动器,解决spring cloud双上下文问题
parent
cbf363789a
commit
d4db30677f
|
@ -36,11 +36,14 @@ import lombok.extern.slf4j.Slf4j;
|
|||
|
||||
/**
|
||||
* hutool方式的定时任务执行
|
||||
* <p>
|
||||
* 使用TimerExeServiceImpl替代,解决spring cloud双上下文执行重复的问题
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/10/27 14:05
|
||||
*/
|
||||
@Slf4j
|
||||
@Deprecated
|
||||
public class HutoolTimerExeServiceImpl implements TimerExeService {
|
||||
|
||||
@Override
|
||||
|
|
|
@ -2,73 +2,51 @@ package cn.stylefeng.roses.kernel.timer.hutool;
|
|||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.cron.CronUtil;
|
||||
import cn.hutool.cron.task.Task;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.stylefeng.roses.kernel.timer.api.TimerAction;
|
||||
import cn.stylefeng.roses.kernel.timer.api.exception.TimerException;
|
||||
import cn.stylefeng.roses.kernel.timer.api.exception.enums.TimerExceptionEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* class description: HuToolTimerExeServiceImpl的改造,
|
||||
* 当引入spring cloud后,需要对原有事件进行改造,使其只执行一次,防止出现错误。
|
||||
* @author xielin
|
||||
* @date 2022/11/11 16:04
|
||||
* HuToolTimerExeServiceImpl的改造
|
||||
* 当引入spring cloud后,需要对原有事件进行改造,使其只执行一次,防止出现错误。
|
||||
*
|
||||
* @author xielin
|
||||
* @date 2022/11/11 16:04
|
||||
*/
|
||||
@Slf4j
|
||||
public class TimerExeServiceImpl extends HutoolTimerExeServiceImpl {
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
|
||||
// 设置秒级别的启用
|
||||
CronUtil.setMatchSecond(true);
|
||||
|
||||
// 启动定时器执行器
|
||||
if (!CronUtil.getScheduler().isStarted()) {
|
||||
CronUtil.start();
|
||||
log.info("scheduler{} is not started",CronUtil.getScheduler().toString());
|
||||
}else{
|
||||
log.info("scheduler{} has already started",CronUtil.getScheduler().toString());
|
||||
log.info("scheduler{} is not started", CronUtil.getScheduler().toString());
|
||||
} else {
|
||||
log.info("scheduler{} has already started", CronUtil.getScheduler().toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startTimer(String taskId, String cron, String className, String params) {
|
||||
|
||||
|
||||
// 判断任务id是否为空
|
||||
if (StrUtil.isBlank(taskId)) {
|
||||
throw new TimerException(TimerExceptionEnum.PARAM_HAS_NULL, "taskId");
|
||||
}
|
||||
|
||||
if (CronUtil.getScheduler().getTask(taskId)!=null){
|
||||
log.info("task ID:{} is existed,then will replace before one",taskId);
|
||||
|
||||
// 如果之前有已经在执行的任务,则停止
|
||||
if (CronUtil.getScheduler().getTask(taskId) != null) {
|
||||
log.info("task ID:{} is existed,then will replace before one", taskId);
|
||||
CronUtil.getScheduler().deschedule(taskId);
|
||||
}
|
||||
|
||||
// 判断任务cron表达式是否为空
|
||||
if (StrUtil.isBlank(cron)) {
|
||||
throw new TimerException(TimerExceptionEnum.PARAM_HAS_NULL, "cron");
|
||||
}
|
||||
|
||||
// 判断类名称是否为空
|
||||
if (StrUtil.isBlank(className)) {
|
||||
throw new TimerException(TimerExceptionEnum.PARAM_HAS_NULL, "className");
|
||||
}
|
||||
|
||||
// 预加载类看是否存在此定时任务类
|
||||
try {
|
||||
Class.forName(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new TimerException(TimerExceptionEnum.CLASS_NOT_FOUND, className);
|
||||
}
|
||||
|
||||
// 定义hutool的任务
|
||||
Task task = () -> {
|
||||
try {
|
||||
TimerAction timerAction = (TimerAction) SpringUtil.getBean(Class.forName(className));
|
||||
timerAction.action(params);
|
||||
} catch (ClassNotFoundException e) {
|
||||
log.error("任务执行异常:{}", e.getMessage());
|
||||
}
|
||||
};
|
||||
|
||||
// 开始执行任务
|
||||
CronUtil.schedule(taskId, cron, task);
|
||||
// 执行以前的启动定时任务的逻辑
|
||||
super.startTimer(taskId, cron, className, params);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,13 +25,10 @@
|
|||
package cn.stylefeng.roses.kernel.timer.starter;
|
||||
|
||||
import cn.stylefeng.roses.kernel.timer.api.TimerExeService;
|
||||
import cn.stylefeng.roses.kernel.timer.hutool.HutoolTimerExeServiceImpl;
|
||||
import cn.stylefeng.roses.kernel.timer.hutool.TimerExeServiceImpl;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
/**
|
||||
* 定时任务的自动配置
|
||||
|
@ -43,27 +40,14 @@ import org.springframework.core.annotation.Order;
|
|||
public class GunsTimerAutoConfiguration {
|
||||
|
||||
/**
|
||||
* hutool的定时任务
|
||||
* hutool 新的定时任务,增加判断spring cloud双上下文
|
||||
*
|
||||
* @author fengshuonan
|
||||
* @date 2020/12/1 21:18
|
||||
* @author xielin
|
||||
* @date 2022/11/15 14:52
|
||||
*/
|
||||
@Bean
|
||||
@Order(value = Ordered.LOWEST_PRECEDENCE)
|
||||
@ConditionalOnMissingBean(TimerExeService.class)
|
||||
public TimerExeService timerExeService() {
|
||||
return new HutoolTimerExeServiceImpl();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* hutool 新的定时任务,增加判断 增加@Order设定注入优先级别
|
||||
* @author xielin
|
||||
* @date 2022/11/15 14:52
|
||||
*/
|
||||
@Bean
|
||||
@Order(value = (Ordered.LOWEST_PRECEDENCE-1))
|
||||
public TimerExeService timerExeService2(){
|
||||
return new TimerExeServiceImpl();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue