代码调整
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
|
||||
package com.njcn.system.timer;
|
||||
|
||||
import com.njcn.system.pojo.param.TimersParam;
|
||||
|
||||
/**
|
||||
* 本接口用来,屏蔽定时任务的多样性
|
||||
* 目前用hutool
|
||||
* @author hongawen
|
||||
*/
|
||||
public interface TimerExeService {
|
||||
|
||||
/**
|
||||
* 启动一个定时器
|
||||
* <p>
|
||||
* 定时任务表达式书写规范:0/2 * * * * *
|
||||
* <p>
|
||||
* 六位数,分别是:秒 分 小时 日 月 年
|
||||
*
|
||||
* @param taskId 任务id
|
||||
* @param cron cron表达式
|
||||
* @param className 类的全名,必须是TimerTaskRunner的子类
|
||||
* @author hongawen
|
||||
*/
|
||||
void startTimer(String taskId, String cron, String className);
|
||||
|
||||
/**
|
||||
* 停止一个定时器
|
||||
*
|
||||
* @param taskId 定时任务Id
|
||||
* @author hongawen
|
||||
*/
|
||||
void stopTimer(String taskId);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
|
||||
package com.njcn.system.timer;
|
||||
|
||||
import cn.hutool.cron.CronUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
|
||||
import com.njcn.common.pojo.enums.common.DataStateEnum;
|
||||
import com.njcn.system.pojo.enums.TimerJobStatusEnum;
|
||||
import com.njcn.system.pojo.param.TimersParam;
|
||||
import com.njcn.system.pojo.po.Timers;
|
||||
import com.njcn.system.service.ITimersService;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author : hongawen
|
||||
*/
|
||||
@Component
|
||||
public class TimerTaskInitListener implements ApplicationRunner, Ordered {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param args incoming application arguments
|
||||
*/
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
ITimersService sysTimersService = SpringUtil.getBean(ITimersService.class);
|
||||
TimerExeService timerExeService = SpringUtil.getBean(TimerExeService.class);
|
||||
// 获取所有开启状态的任务
|
||||
LambdaQueryWrapper<Timers> lambdaQueryChainWrapper = new LambdaQueryWrapper<>();
|
||||
lambdaQueryChainWrapper.eq(Timers::getJobStatus,TimerJobStatusEnum.RUNNING.getCode())
|
||||
.ne(Timers::getState, DataStateEnum.DELETED.getCode());
|
||||
List<Timers> list = sysTimersService.list(lambdaQueryChainWrapper);
|
||||
// 添加定时任务到调度器
|
||||
for (Timers sysTimers : list) {
|
||||
timerExeService.startTimer(String.valueOf(sysTimers.getId()), sysTimers.getCron(), sysTimers.getActionClass());
|
||||
}
|
||||
|
||||
// 设置秒级别的启用
|
||||
CronUtil.setMatchSecond(true);
|
||||
|
||||
// 启动定时器执行器
|
||||
CronUtil.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return LOWEST_PRECEDENCE-10;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
|
||||
package com.njcn.system.timer;
|
||||
|
||||
/**
|
||||
* 定时器执行者
|
||||
* <p>
|
||||
* 定时器都要实现本接口,并需要把实现类加入到spring容器中
|
||||
*
|
||||
*/
|
||||
public interface TimerTaskRunner {
|
||||
|
||||
/**
|
||||
* 任务执行的具体内容
|
||||
*
|
||||
* @author hongawen
|
||||
*/
|
||||
void action();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
|
||||
package com.njcn.system.timer.impl;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.cron.CronUtil;
|
||||
import cn.hutool.cron.task.Task;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import cn.hutool.log.Log;
|
||||
import cn.hutool.setting.Setting;
|
||||
import com.njcn.common.pojo.exception.BusinessException;
|
||||
import com.njcn.system.enums.SystemResponseEnum;
|
||||
import com.njcn.system.timer.TimerExeService;
|
||||
import com.njcn.system.timer.TimerTaskRunner;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* hutool方式的定时任务执行
|
||||
*
|
||||
* @author hongawen
|
||||
*/
|
||||
@Service
|
||||
public class HutoolTimerExeServiceImpl implements TimerExeService {
|
||||
|
||||
private static final Log log = Log.get();
|
||||
|
||||
@Override
|
||||
public void startTimer(String taskId, String cron, String className) {
|
||||
|
||||
if (ObjectUtil.hasEmpty(taskId, cron, className)) {
|
||||
throw new BusinessException(SystemResponseEnum.EXE_EMPTY_PARAM);
|
||||
}
|
||||
|
||||
// 预加载类看是否存在此定时任务类
|
||||
try {
|
||||
Class.forName(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new BusinessException(SystemResponseEnum.TIMER_NOT_EXISTED);
|
||||
}
|
||||
|
||||
// 定义hutool的任务
|
||||
Task task = () -> {
|
||||
try {
|
||||
TimerTaskRunner timerTaskRunner = (TimerTaskRunner) SpringUtil.getBean(Class.forName(className));
|
||||
timerTaskRunner.action();
|
||||
} catch (ClassNotFoundException e) {
|
||||
log.error(">>> 任务执行异常:{}", e.getMessage());
|
||||
}
|
||||
};
|
||||
|
||||
// 开始执行任务
|
||||
CronUtil.schedule(taskId, cron, task);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stopTimer(String taskId) {
|
||||
CronUtil.remove(taskId);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
|
||||
package com.njcn.system.timer.tasks;
|
||||
|
||||
import com.njcn.system.timer.TimerTaskRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
/**
|
||||
* 定时同步用户数据
|
||||
*
|
||||
* @author hongawen
|
||||
* @date 2020/6/30 22:09
|
||||
*/
|
||||
@Component
|
||||
public class DemoTaskRunner implements TimerTaskRunner {
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void action() {
|
||||
|
||||
//定时同步用户数据
|
||||
System.out.println("定时输出一段文字");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user