mirror of https://gitee.com/stylefeng/roses
定时任务添加参数
parent
23f1128fe8
commit
e12bf8e7eb
|
@ -33,7 +33,7 @@ public class ClearInvalidLoginUserCacheTimer implements TimerAction {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void action() {
|
||||
public void action(String params) {
|
||||
Collection<String> allOnlineUsers = allPlaceLoginTokenCache.getAllKeys();
|
||||
if (ObjectUtil.isNotEmpty(allOnlineUsers)) {
|
||||
for (String userId : allOnlineUsers) {
|
||||
|
|
|
@ -37,9 +37,10 @@ public interface TimerAction {
|
|||
/**
|
||||
* 任务执行的具体内容
|
||||
*
|
||||
* @param params 任务参数
|
||||
* @author stylefeng
|
||||
* @date 2020/6/28 21:29
|
||||
*/
|
||||
void action();
|
||||
void action(String params);
|
||||
|
||||
}
|
||||
|
|
|
@ -58,10 +58,11 @@ public interface TimerExeService {
|
|||
* @param taskId 任务id
|
||||
* @param cron cron表达式
|
||||
* @param className 类的全名,必须是TimerAction的子类
|
||||
* @param params 自定义参数
|
||||
* @author stylefeng
|
||||
* @date 2020/7/1 13:51
|
||||
*/
|
||||
void startTimer(String taskId, String cron, String className);
|
||||
void startTimer(String taskId, String cron, String className, String params);
|
||||
|
||||
/**
|
||||
* 停止一个定时器
|
||||
|
|
|
@ -64,6 +64,12 @@ public class SysTimers extends BaseEntity {
|
|||
@TableField("cron")
|
||||
private String cron;
|
||||
|
||||
/**
|
||||
* 参数
|
||||
*/
|
||||
@TableField("params")
|
||||
private String params;
|
||||
|
||||
/**
|
||||
* 状态:1-运行,2-停止
|
||||
*/
|
||||
|
|
|
@ -63,7 +63,7 @@ public class TaskRunListener implements ApplicationListener<ApplicationStartedEv
|
|||
// 添加定时任务到调度器
|
||||
for (SysTimers sysTimers : list) {
|
||||
try {
|
||||
timerExeService.startTimer(String.valueOf(sysTimers.getTimerId()), sysTimers.getCron(), sysTimers.getActionClass());
|
||||
timerExeService.startTimer(String.valueOf(sysTimers.getTimerId()), sysTimers.getCron(), sysTimers.getActionClass(), sysTimers.getParams());
|
||||
} catch (Exception exception) {
|
||||
// 遇到错误直接略过这个定时器(可能多个项目公用库)
|
||||
log.error("定时器初始化遇到错误,略过该定时器!", exception);
|
||||
|
|
|
@ -71,6 +71,11 @@ public class SysTimersParam extends BaseRequest {
|
|||
@NotNull(message = "任务状态不能为空", groups = {edit.class})
|
||||
private Integer jobStatus;
|
||||
|
||||
/**
|
||||
* 参数
|
||||
*/
|
||||
private String params;
|
||||
|
||||
/**
|
||||
* 备注信息
|
||||
*/
|
||||
|
|
|
@ -29,6 +29,7 @@ import cn.hutool.core.collection.CollectionUtil;
|
|||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.cron.CronUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.hutool.http.HtmlUtil;
|
||||
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
|
||||
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
|
||||
import cn.stylefeng.roses.kernel.db.api.pojo.entity.BaseEntity;
|
||||
|
@ -70,6 +71,8 @@ public class SysTimersServiceImpl extends ServiceImpl<SysTimersMapper, SysTimers
|
|||
|
||||
@Override
|
||||
public void add(SysTimersParam sysTimersParam) {
|
||||
// 还原被转义的HTML特殊字符
|
||||
unescapeHtml(sysTimersParam);
|
||||
|
||||
// 将dto转为实体
|
||||
SysTimers sysTimers = new SysTimers();
|
||||
|
@ -97,6 +100,8 @@ public class SysTimersServiceImpl extends ServiceImpl<SysTimersMapper, SysTimers
|
|||
@Transactional(rollbackFor = Exception.class)
|
||||
@Override
|
||||
public void edit(SysTimersParam sysTimersParam) {
|
||||
// 还原被转义的HTML特殊字符
|
||||
unescapeHtml(sysTimersParam);
|
||||
|
||||
// 更新库中记录
|
||||
SysTimers oldTimer = this.querySysTimers(sysTimersParam);
|
||||
|
@ -109,10 +114,7 @@ public class SysTimersServiceImpl extends ServiceImpl<SysTimersMapper, SysTimers
|
|||
// 如果任务正在运行,则停掉这个任务,从新运行任务
|
||||
if (jobStatus.equals(TimerJobStatusEnum.RUNNING.getCode())) {
|
||||
CronUtil.remove(String.valueOf(oldTimer.getTimerId()));
|
||||
timerExeService.startTimer(
|
||||
String.valueOf(sysTimersParam.getTimerId()),
|
||||
sysTimersParam.getCron(),
|
||||
sysTimersParam.getActionClass());
|
||||
timerExeService.startTimer(String.valueOf(sysTimersParam.getTimerId()), sysTimersParam.getCron(), sysTimersParam.getActionClass(), sysTimersParam.getParams());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -122,13 +124,12 @@ public class SysTimersServiceImpl extends ServiceImpl<SysTimersMapper, SysTimers
|
|||
|
||||
// 更新库中的状态
|
||||
LambdaUpdateWrapper<SysTimers> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.set(SysTimers::getJobStatus, TimerJobStatusEnum.RUNNING.getCode())
|
||||
.eq(SysTimers::getTimerId, sysTimersParam.getTimerId());
|
||||
wrapper.set(SysTimers::getJobStatus, TimerJobStatusEnum.RUNNING.getCode()).eq(SysTimers::getTimerId, sysTimersParam.getTimerId());
|
||||
this.update(wrapper);
|
||||
|
||||
// 添加定时任务调度
|
||||
SysTimers sysTimers = this.querySysTimers(sysTimersParam);
|
||||
timerExeService.startTimer(String.valueOf(sysTimers.getTimerId()), sysTimers.getCron(), sysTimers.getActionClass());
|
||||
timerExeService.startTimer(String.valueOf(sysTimers.getTimerId()), sysTimers.getCron(), sysTimers.getActionClass(), sysTimers.getParams());
|
||||
}
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
|
@ -137,8 +138,7 @@ public class SysTimersServiceImpl extends ServiceImpl<SysTimersMapper, SysTimers
|
|||
|
||||
// 更新库中的状态
|
||||
LambdaUpdateWrapper<SysTimers> wrapper = new LambdaUpdateWrapper<>();
|
||||
wrapper.set(SysTimers::getJobStatus, TimerJobStatusEnum.STOP.getCode())
|
||||
.eq(SysTimers::getTimerId, sysTimersParam.getTimerId());
|
||||
wrapper.set(SysTimers::getJobStatus, TimerJobStatusEnum.STOP.getCode()).eq(SysTimers::getTimerId, sysTimersParam.getTimerId());
|
||||
this.update(wrapper);
|
||||
|
||||
// 关闭定时任务调度
|
||||
|
@ -231,4 +231,17 @@ public class SysTimersServiceImpl extends ServiceImpl<SysTimersMapper, SysTimers
|
|||
return queryWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 还原被转义的HTML特殊字符
|
||||
*
|
||||
* @param sysTimersParam 定时任务参数
|
||||
* @author luojie
|
||||
* @date 2021/04/09 21:40
|
||||
*/
|
||||
private void unescapeHtml(SysTimersParam sysTimersParam) {
|
||||
String params = sysTimersParam.getParams();
|
||||
params = HtmlUtil.unescape(params);
|
||||
sysTimersParam.setParams(params);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
*/
|
||||
package cn.stylefeng.roses.kernel.timer.modular.tasks;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.stylefeng.roses.kernel.timer.api.TimerAction;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
@ -37,8 +38,8 @@ import org.springframework.stereotype.Component;
|
|||
public class SystemOutTaskRunner implements TimerAction {
|
||||
|
||||
@Override
|
||||
public void action() {
|
||||
System.out.println("这是一个定时任务测试的程序,一直输出这行内容!");
|
||||
public void action(String params) {
|
||||
System.out.println(StrUtil.format("这是一个定时任务测试的程序,一直输出这行内容!这个是参数: {}", params));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@ public class HutoolTimerExeServiceImpl implements TimerExeService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void startTimer(String taskId, String cron, String className) {
|
||||
public void startTimer(String taskId, String cron, String className, String params) {
|
||||
|
||||
// 判断任务id是否为空
|
||||
if (StrUtil.isBlank(taskId)) {
|
||||
|
@ -86,7 +86,7 @@ public class HutoolTimerExeServiceImpl implements TimerExeService {
|
|||
Task task = () -> {
|
||||
try {
|
||||
TimerAction timerAction = (TimerAction) SpringUtil.getBean(Class.forName(className));
|
||||
timerAction.action();
|
||||
timerAction.action(params);
|
||||
} catch (ClassNotFoundException e) {
|
||||
log.error("任务执行异常:{}", e.getMessage());
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ public class SystemHardwareInfoHolder implements TimerAction {
|
|||
private SystemHardwareCalculator systemHardwareCalculator = null;
|
||||
|
||||
@Override
|
||||
public void action() {
|
||||
public void action(String params) {
|
||||
SystemHardwareCalculator newInfo = new SystemHardwareCalculator();
|
||||
newInfo.calc();
|
||||
systemHardwareCalculator = newInfo;
|
||||
|
|
Loading…
Reference in New Issue