mirror of https://gitee.com/stylefeng/roses
【timer】更新定时任务
parent
5df2221389
commit
3c0a528f31
|
@ -8,6 +8,22 @@ package cn.stylefeng.roses.kernel.timer.api;
|
||||||
*/
|
*/
|
||||||
public interface TimerExeService {
|
public interface TimerExeService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 开启定时器调度
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2021/1/12 20:33
|
||||||
|
*/
|
||||||
|
void start();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭定时器调度
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2021/1/12 20:33
|
||||||
|
*/
|
||||||
|
void stop();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动一个定时器
|
* 启动一个定时器
|
||||||
* <p>
|
* <p>
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
package cn.stylefeng.roses.kernel.timer.modular.listener;
|
||||||
|
|
||||||
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
|
import cn.stylefeng.roses.kernel.timer.api.TimerExeService;
|
||||||
|
import cn.stylefeng.roses.kernel.timer.api.enums.TimerJobStatusEnum;
|
||||||
|
import cn.stylefeng.roses.kernel.timer.modular.entity.SysTimers;
|
||||||
|
import cn.stylefeng.roses.kernel.timer.modular.param.SysTimersParam;
|
||||||
|
import cn.stylefeng.roses.kernel.timer.modular.service.SysTimersService;
|
||||||
|
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 项目启动,将数据库所有定时任务开启
|
||||||
|
*
|
||||||
|
* @author fengshuonan
|
||||||
|
* @date 2021/1/12 20:40
|
||||||
|
*/
|
||||||
|
public class TaskRunListener implements ApplicationListener<ApplicationStartedEvent>, Ordered {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ApplicationStartedEvent event) {
|
||||||
|
|
||||||
|
SysTimersService sysTimersService = SpringUtil.getBean(SysTimersService.class);
|
||||||
|
TimerExeService timerExeService = SpringUtil.getBean(TimerExeService.class);
|
||||||
|
|
||||||
|
// 开启任务调度
|
||||||
|
timerExeService.start();
|
||||||
|
|
||||||
|
// 获取数据库所有开启状态的任务
|
||||||
|
SysTimersParam sysTimersParam = new SysTimersParam();
|
||||||
|
sysTimersParam.setJobStatus(TimerJobStatusEnum.RUNNING.getCode());
|
||||||
|
List<SysTimers> list = sysTimersService.list(sysTimersParam);
|
||||||
|
|
||||||
|
// 添加定时任务到调度器
|
||||||
|
for (SysTimers sysTimers : list) {
|
||||||
|
timerExeService.startTimer(String.valueOf(sysTimers.getTimerId()), sysTimers.getCron(), sysTimers.getActionClass());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return LOWEST_PRECEDENCE;
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,6 +19,20 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class HutoolTimerExeServiceImpl implements TimerExeService {
|
public class HutoolTimerExeServiceImpl implements TimerExeService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start() {
|
||||||
|
// 设置秒级别的启用
|
||||||
|
CronUtil.setMatchSecond(true);
|
||||||
|
|
||||||
|
// 启动定时器执行器
|
||||||
|
CronUtil.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
CronUtil.stop();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startTimer(String taskId, String cron, String className) {
|
public void startTimer(String taskId, String cron, String className) {
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,4 @@
|
||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||||
cn.stylefeng.roses.kernel.timer.starter.GunsTimerAutoConfiguration
|
cn.stylefeng.roses.kernel.timer.starter.GunsTimerAutoConfiguration
|
||||||
|
org.springframework.context.ApplicationListener=\
|
||||||
|
cn.stylefeng.roses.kernel.timer.modular.listener.TaskRunListener
|
||||||
|
|
Loading…
Reference in New Issue