!43 Fix #l618xz problem

* For #l618xz, Fix timer started repeat problem/bug。
* Merge remote-tracking branch 'remotes/origin/dev-7.3.2' into develop-i…
* For #l618xz, Fix timer started repeat problem/bug。
pull/41/MERGE
xielin 2022-11-19 14:38:27 +00:00 committed by stylefeng
parent 1996ee1421
commit cbf363789a
2 changed files with 90 additions and 0 deletions

View File

@ -0,0 +1,74 @@
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
*/
@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());
}
}
@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);
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);
}
}

View File

@ -26,9 +26,12 @@ 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;
/**
*
@ -46,9 +49,22 @@ public class GunsTimerAutoConfiguration {
* @date 2020/12/1 21:18
*/
@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();
}
}