From cbf363789a00eed44f60b1c4bfccde93e89656d9 Mon Sep 17 00:00:00 2001 From: xielin <2492617563@qq.com> Date: Sat, 19 Nov 2022 14:38:27 +0000 Subject: [PATCH] =?UTF-8?q?!43=20Fix=20#l618xz=20problem=20*=20For=20#l618?= =?UTF-8?q?xz,=20Fix=20timer=20started=20repeat=20problem/bug=E3=80=82=20*?= =?UTF-8?q?=20Merge=20remote-tracking=20branch=20'remotes/origin/dev-7.3.2?= =?UTF-8?q?'=20into=20develop-i=E2=80=A6=20*=20For=20#l618xz,=20Fix=20time?= =?UTF-8?q?r=20started=20repeat=20problem/bug=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../timer/hutool/TimerExeServiceImpl.java | 74 +++++++++++++++++++ .../starter/GunsTimerAutoConfiguration.java | 16 ++++ 2 files changed, 90 insertions(+) create mode 100644 kernel-d-timer/timer-sdk-hutool/src/main/java/cn/stylefeng/roses/kernel/timer/hutool/TimerExeServiceImpl.java diff --git a/kernel-d-timer/timer-sdk-hutool/src/main/java/cn/stylefeng/roses/kernel/timer/hutool/TimerExeServiceImpl.java b/kernel-d-timer/timer-sdk-hutool/src/main/java/cn/stylefeng/roses/kernel/timer/hutool/TimerExeServiceImpl.java new file mode 100644 index 000000000..2fda18e71 --- /dev/null +++ b/kernel-d-timer/timer-sdk-hutool/src/main/java/cn/stylefeng/roses/kernel/timer/hutool/TimerExeServiceImpl.java @@ -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 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); + } +} diff --git a/kernel-d-timer/timer-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/timer/starter/GunsTimerAutoConfiguration.java b/kernel-d-timer/timer-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/timer/starter/GunsTimerAutoConfiguration.java index a2d2f7e5e..f187b7bfe 100644 --- a/kernel-d-timer/timer-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/timer/starter/GunsTimerAutoConfiguration.java +++ b/kernel-d-timer/timer-spring-boot-starter/src/main/java/cn/stylefeng/roses/kernel/timer/starter/GunsTimerAutoConfiguration.java @@ -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(); + } }