feat: There is no way the, 动态定时任务
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package com.adc.da.search.conf;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @author: super_liu
|
||||
* @date: 2022年05月27日 10:17
|
||||
*/
|
||||
|
||||
import com.adc.da.slrs.tsTask.dao.TbTaskDao;
|
||||
import com.adc.da.slrs.tsTask.entity.TbTask;
|
||||
import com.adc.da.slrs.tsTask.service.ITbTaskService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.support.CronTrigger;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
|
||||
/**
|
||||
* 动态定时器
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class SchedulerConf {
|
||||
|
||||
//数据库的任务
|
||||
public static ConcurrentHashMap<String, TbTask> tasks = new ConcurrentHashMap<>(10);
|
||||
|
||||
//正在运行的任务
|
||||
public static ConcurrentHashMap<String, ScheduledFuture> runTasks = new ConcurrentHashMap<>(10);
|
||||
|
||||
//线程池任务调度
|
||||
private ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
|
||||
|
||||
@Autowired
|
||||
TbTaskDao tbTaskDao;
|
||||
|
||||
@Autowired
|
||||
ITbTaskService iTbTaskService;
|
||||
|
||||
/**
|
||||
* 初始化线程池任务调度
|
||||
*/
|
||||
@Autowired
|
||||
public SchedulerConf(){
|
||||
this.threadPoolTaskScheduler.setPoolSize(10);
|
||||
this.threadPoolTaskScheduler.setThreadNamePrefix("task-thread-");
|
||||
this.threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
|
||||
this.threadPoolTaskScheduler.initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有数据库里的定时任务
|
||||
*/
|
||||
private void getAllTbTask(){
|
||||
//查询所有,并put到tasks
|
||||
SchedulerConf.tasks.clear();
|
||||
List<TbTask> list = iTbTaskService.list();
|
||||
list.forEach((task)-> SchedulerConf.tasks.put(task.getId(),task));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据定时任务id,启动定时任务
|
||||
*/
|
||||
public void start(String taskId){
|
||||
try {
|
||||
//如果为空,重新获取
|
||||
if(SchedulerConf.tasks.size() <= 0){
|
||||
this.getAllTbTask();
|
||||
}
|
||||
TbTask tbTask = SchedulerConf.tasks.get(taskId);
|
||||
|
||||
//获取并实例化Runnable任务类
|
||||
Class<?> clazz = Class.forName(tbTask.getTaskClass());
|
||||
Runnable runnable = (Runnable)clazz.newInstance();
|
||||
|
||||
//Cron表达式
|
||||
CronTrigger cron = new CronTrigger(tbTask.getTaskExp());
|
||||
|
||||
//执行,并put到runTasks
|
||||
SchedulerConf.runTasks.put(taskId, Objects.requireNonNull(this.threadPoolTaskScheduler.schedule(runnable, cron)));
|
||||
|
||||
this.updateTaskStatus(taskId,1);
|
||||
|
||||
log.info("{},任务启动!",taskId);
|
||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
|
||||
log.error("{},任务启动失败...",taskId);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据定时任务id,停止定时任务
|
||||
*/
|
||||
public void stop(String taskId){
|
||||
SchedulerConf.runTasks.get(taskId).cancel(true);
|
||||
|
||||
SchedulerConf.runTasks.remove(taskId);
|
||||
|
||||
this.updateTaskStatus(taskId,0);
|
||||
|
||||
log.info("{},任务停止...",taskId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新数据库动态定时任务状态
|
||||
*/
|
||||
private void updateTaskStatus(String taskId,int status){
|
||||
TbTask task = iTbTaskService.getById(taskId);
|
||||
task.setTaskStatus(status);
|
||||
task.setUpdateTime(new Date());
|
||||
iTbTaskService.updateById(task);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user