From 16b4e825fbf21917790df3879e1974bcecb8e737 Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Mon, 8 Feb 2021 10:44:28 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90timer=E3=80=91=E6=95=B4=E7=90=86?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../timer/api/exception/TimerException.java | 5 +- .../controller/SysTimersController.java | 24 +++---- .../modular/service/SysTimersService.java | 18 +++--- .../service/impl/SysTimersServiceImpl.java | 64 +++++++++---------- .../hutool/HutoolTimerExeServiceImpl.java | 12 ++-- 5 files changed, 59 insertions(+), 64 deletions(-) diff --git a/kernel-d-timer/timer-api/src/main/java/cn/stylefeng/roses/kernel/timer/api/exception/TimerException.java b/kernel-d-timer/timer-api/src/main/java/cn/stylefeng/roses/kernel/timer/api/exception/TimerException.java index f894658ab..bf8f2064b 100644 --- a/kernel-d-timer/timer-api/src/main/java/cn/stylefeng/roses/kernel/timer/api/exception/TimerException.java +++ b/kernel-d-timer/timer-api/src/main/java/cn/stylefeng/roses/kernel/timer/api/exception/TimerException.java @@ -1,5 +1,6 @@ package cn.stylefeng.roses.kernel.timer.api.exception; +import cn.hutool.core.util.StrUtil; import cn.stylefeng.roses.kernel.rule.exception.AbstractExceptionEnum; import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException; import cn.stylefeng.roses.kernel.timer.api.constants.TimerConstants; @@ -16,8 +17,8 @@ public class TimerException extends ServiceException { super(TimerConstants.TIMER_MODULE_NAME, exception); } - public TimerException(AbstractExceptionEnum exception, String userTip) { - super(TimerConstants.TIMER_MODULE_NAME, exception.getErrorCode(), userTip); + public TimerException(AbstractExceptionEnum exception, Object... params) { + super(TimerConstants.TIMER_MODULE_NAME, exception.getErrorCode(), StrUtil.format(exception.getUserTip(), params)); } } diff --git a/kernel-d-timer/timer-business/src/main/java/cn/stylefeng/roses/kernel/timer/modular/controller/SysTimersController.java b/kernel-d-timer/timer-business/src/main/java/cn/stylefeng/roses/kernel/timer/modular/controller/SysTimersController.java index b9a5da9ff..079d55a02 100644 --- a/kernel-d-timer/timer-business/src/main/java/cn/stylefeng/roses/kernel/timer/modular/controller/SysTimersController.java +++ b/kernel-d-timer/timer-business/src/main/java/cn/stylefeng/roses/kernel/timer/modular/controller/SysTimersController.java @@ -39,18 +39,6 @@ public class SysTimersController { return new SuccessResponseData(); } - /** - * 编辑定时任务 - * - * @author stylefeng - * @date 2020/6/30 18:26 - */ - @PostResource(name = "编辑定时任务", path = "/sysTimers/edit") - public ResponseData edit(@RequestBody @Validated(SysTimersParam.edit.class) SysTimersParam sysTimersParam) { - sysTimersService.edit(sysTimersParam); - return new SuccessResponseData(); - } - /** * 删除定时任务 * @@ -63,6 +51,18 @@ public class SysTimersController { return new SuccessResponseData(); } + /** + * 编辑定时任务 + * + * @author stylefeng + * @date 2020/6/30 18:26 + */ + @PostResource(name = "编辑定时任务", path = "/sysTimers/edit") + public ResponseData edit(@RequestBody @Validated(SysTimersParam.edit.class) SysTimersParam sysTimersParam) { + sysTimersService.edit(sysTimersParam); + return new SuccessResponseData(); + } + /** * 启动定时任务 * diff --git a/kernel-d-timer/timer-business/src/main/java/cn/stylefeng/roses/kernel/timer/modular/service/SysTimersService.java b/kernel-d-timer/timer-business/src/main/java/cn/stylefeng/roses/kernel/timer/modular/service/SysTimersService.java index 885351e67..f4624f65f 100644 --- a/kernel-d-timer/timer-business/src/main/java/cn/stylefeng/roses/kernel/timer/modular/service/SysTimersService.java +++ b/kernel-d-timer/timer-business/src/main/java/cn/stylefeng/roses/kernel/timer/modular/service/SysTimersService.java @@ -24,15 +24,6 @@ public interface SysTimersService extends IService { */ void add(SysTimersParam sysTimersParam); - /** - * 编辑定时任务 - * - * @param sysTimersParam 编辑参数 - * @author stylefeng - * @date 2020/6/30 18:26 - */ - void edit(SysTimersParam sysTimersParam); - /** * 删除定时任务 * @@ -42,6 +33,15 @@ public interface SysTimersService extends IService { */ void del(SysTimersParam sysTimersParam); + /** + * 编辑定时任务 + * + * @param sysTimersParam 编辑参数 + * @author stylefeng + * @date 2020/6/30 18:26 + */ + void edit(SysTimersParam sysTimersParam); + /** * 启动任务 * diff --git a/kernel-d-timer/timer-business/src/main/java/cn/stylefeng/roses/kernel/timer/modular/service/impl/SysTimersServiceImpl.java b/kernel-d-timer/timer-business/src/main/java/cn/stylefeng/roses/kernel/timer/modular/service/impl/SysTimersServiceImpl.java index 3af4d419c..26017bf14 100644 --- a/kernel-d-timer/timer-business/src/main/java/cn/stylefeng/roses/kernel/timer/modular/service/impl/SysTimersServiceImpl.java +++ b/kernel-d-timer/timer-business/src/main/java/cn/stylefeng/roses/kernel/timer/modular/service/impl/SysTimersServiceImpl.java @@ -3,7 +3,6 @@ package cn.stylefeng.roses.kernel.timer.modular.service.impl; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ObjectUtil; -import cn.hutool.core.util.StrUtil; import cn.hutool.cron.CronUtil; import cn.hutool.extra.spring.SpringUtil; import cn.stylefeng.roses.kernel.db.api.factory.PageFactory; @@ -28,8 +27,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import javax.validation.constraints.NotBlank; -import javax.validation.constraints.NotNull; import java.util.Collection; import java.util.List; import java.util.Map; @@ -60,6 +57,19 @@ public class SysTimersServiceImpl extends ServiceImpl updateWrapper = new LambdaUpdateWrapper<>(); + updateWrapper.set(SysTimers::getDelFlag, YesOrNotEnum.Y.getCode()); + updateWrapper.eq(SysTimers::getTimerId, sysTimersParam.getTimerId()); + this.update(updateWrapper); + } + @Transactional(rollbackFor = Exception.class) @Override public void edit(SysTimersParam sysTimersParam) { @@ -82,19 +92,6 @@ public class SysTimersServiceImpl extends ServiceImpl updateWrapper = new LambdaUpdateWrapper<>(); - updateWrapper.set(SysTimers::getDelFlag, YesOrNotEnum.Y.getCode()); - updateWrapper.eq(SysTimers::getTimerId, sysTimersParam.getTimerId()); - this.update(updateWrapper); - } - @Transactional(rollbackFor = Exception.class) @Override public void start(SysTimersParam sysTimersParam) { @@ -169,8 +166,7 @@ public class SysTimersServiceImpl extends ServiceImpl queryWrapper = new LambdaQueryWrapper<>(); - if (ObjectUtil.isNotNull(sysTimersParam)) { - String timerName = sysTimersParam.getTimerName(); - Integer jobStatus = sysTimersParam.getJobStatus(); - String actionClass = sysTimersParam.getActionClass(); - - // 拼接查询条件-任务名称 - queryWrapper.like(ObjectUtil.isNotEmpty(timerName), SysTimers::getTimerName, timerName); - - // 拼接查询条件-状态(字典 1运行 2停止) - queryWrapper.like(ObjectUtil.isNotNull(jobStatus), SysTimers::getJobStatus, jobStatus); - - // 拼接查询条件-类名 - queryWrapper.like(ObjectUtil.isNotEmpty(actionClass), SysTimers::getActionClass, actionClass); - } - // 查询未删除的 queryWrapper.ne(SysTimers::getDelFlag, YesOrNotEnum.Y.getCode()); // 按类型升序排列,同类型的排在一起 queryWrapper.orderByDesc(BaseEntity::getCreateTime); + if (ObjectUtil.isEmpty(sysTimersParam)) { + return queryWrapper; + } + + String timerName = sysTimersParam.getTimerName(); + Integer jobStatus = sysTimersParam.getJobStatus(); + String actionClass = sysTimersParam.getActionClass(); + + // 拼接查询条件-任务名称 + queryWrapper.like(ObjectUtil.isNotEmpty(timerName), SysTimers::getTimerName, timerName); + + // 拼接查询条件-状态(字典 1运行 2停止) + queryWrapper.like(ObjectUtil.isNotNull(jobStatus), SysTimers::getJobStatus, jobStatus); + + // 拼接查询条件-类名 + queryWrapper.like(ObjectUtil.isNotEmpty(actionClass), SysTimers::getActionClass, actionClass); + return queryWrapper; } diff --git a/kernel-d-timer/timer-sdk-hutool/src/main/java/cn/stylefeng/roses/kernel/hutool/HutoolTimerExeServiceImpl.java b/kernel-d-timer/timer-sdk-hutool/src/main/java/cn/stylefeng/roses/kernel/hutool/HutoolTimerExeServiceImpl.java index c3eb0d96f..3e6867e0d 100644 --- a/kernel-d-timer/timer-sdk-hutool/src/main/java/cn/stylefeng/roses/kernel/hutool/HutoolTimerExeServiceImpl.java +++ b/kernel-d-timer/timer-sdk-hutool/src/main/java/cn/stylefeng/roses/kernel/hutool/HutoolTimerExeServiceImpl.java @@ -38,28 +38,24 @@ public class HutoolTimerExeServiceImpl implements TimerExeService { // 判断任务id是否为空 if (StrUtil.isBlank(taskId)) { - String userTip = StrUtil.format(TimerExceptionEnum.PARAM_HAS_NULL.getUserTip(), "taskId"); - throw new TimerException(TimerExceptionEnum.PARAM_HAS_NULL, userTip); + throw new TimerException(TimerExceptionEnum.PARAM_HAS_NULL, "taskId"); } // 判断任务cron表达式是否为空 if (StrUtil.isBlank(cron)) { - String userTip = StrUtil.format(TimerExceptionEnum.PARAM_HAS_NULL.getUserTip(), "cron"); - throw new TimerException(TimerExceptionEnum.PARAM_HAS_NULL, userTip); + throw new TimerException(TimerExceptionEnum.PARAM_HAS_NULL, "cron"); } // 判断类名称是否为空 if (StrUtil.isBlank(className)) { - String userTip = StrUtil.format(TimerExceptionEnum.PARAM_HAS_NULL.getUserTip(), "className"); - throw new TimerException(TimerExceptionEnum.PARAM_HAS_NULL, userTip); + throw new TimerException(TimerExceptionEnum.PARAM_HAS_NULL, "className"); } // 预加载类看是否存在此定时任务类 try { Class.forName(className); } catch (ClassNotFoundException e) { - String userTip = StrUtil.format(TimerExceptionEnum.CLASS_NOT_FOUND.getUserTip(), className); - throw new TimerException(TimerExceptionEnum.CLASS_NOT_FOUND, userTip); + throw new TimerException(TimerExceptionEnum.CLASS_NOT_FOUND, className); } // 定义hutool的任务