【7.3.2】【timer】修复定时任务启动器,解决spring cloud双上下文问题

pull/41/MERGE
fengshuonan 2022-11-19 22:53:50 +08:00
parent cbf363789a
commit d4db30677f
3 changed files with 27 additions and 62 deletions

View File

@ -36,11 +36,14 @@ import lombok.extern.slf4j.Slf4j;
/**
* hutool
* <p>
* 使TimerExeServiceImplspring cloud
*
* @author fengshuonan
* @date 2020/10/27 14:05
*/
@Slf4j
@Deprecated
public class HutoolTimerExeServiceImpl implements TimerExeService {
@Override

View File

@ -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 existedthen will replace before one",taskId);
// 如果之前有已经在执行的任务,则停止
if (CronUtil.getScheduler().getTask(taskId) != null) {
log.info("task ID:{} is existedthen 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);
}
}

View File

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