【timer】整理定时任务

pull/3/head
fengshuonan 2021-02-08 10:44:28 +08:00
parent 67b8b81a48
commit 16b4e825fb
5 changed files with 59 additions and 64 deletions

View File

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

View File

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

View File

@ -24,15 +24,6 @@ public interface SysTimersService extends IService<SysTimers> {
*/
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<SysTimers> {
*/
void del(SysTimersParam sysTimersParam);
/**
*
*
* @param sysTimersParam
* @author stylefeng
* @date 2020/6/30 18:26
*/
void edit(SysTimersParam sysTimersParam);
/**
*
*

View File

@ -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<SysTimersMapper, SysTimers
this.save(sysTimers);
}
@Override
public void del(SysTimersParam sysTimersParam) {
// 先停止id为参数id的定时器
CronUtil.remove(String.valueOf(sysTimersParam.getTimerId()));
// 逻辑删除定时任务
LambdaUpdateWrapper<SysTimers> 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<SysTimersMapper, SysTimers
}
}
@Override
public void del(SysTimersParam sysTimersParam) {
// 先停止id为参数id的定时器
CronUtil.remove(String.valueOf(sysTimersParam.getTimerId()));
// 逻辑删除定时任务
LambdaUpdateWrapper<SysTimers> 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<SysTimersMapper, SysTimers
private SysTimers querySysTimers(SysTimersParam sysTimersParam) {
SysTimers sysTimers = this.getById(sysTimersParam.getTimerId());
if (ObjectUtil.isEmpty(sysTimers) || sysTimers.getDelFlag().equals(YesOrNotEnum.Y.getCode())) {
String userTip = StrUtil.format(TimerExceptionEnum.JOB_DETAIL_NOT_FOUND.getUserTip(), sysTimersParam.getTimerId());
throw new TimerException(TimerExceptionEnum.JOB_DETAIL_NOT_FOUND, userTip);
throw new TimerException(TimerExceptionEnum.JOB_DETAIL_NOT_FOUND, sysTimersParam.getTimerId());
}
return sysTimers;
}
@ -185,27 +181,29 @@ public class SysTimersServiceImpl extends ServiceImpl<SysTimersMapper, SysTimers
LambdaQueryWrapper<SysTimers> 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;
}

View File

@ -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的任务