此文章是主要是动态的将任务根据cron表达式进行管理定时任务,具体任务执行的内容或者类已经指定写好,以后有需要不同的任务执行类动态管理的话,再过来进行补充
1.注入 taskScheduler
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@Configuration
public class ScheduledConfig {
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler scheduling = new ThreadPoolTaskScheduler();
scheduling.setPoolSize(5);
scheduling.initialize();
return scheduling;
}
}
2.定时任务管理
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.ScheduledTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
@Slf4j
@Service
public class DynamicScheduledTaskRegistrar extends ScheduledTaskRegistrar {
private final Map<String, ScheduledTask> scheduledTaskMap = new ConcurrentHashMap<>();
@Autowired
TaskScheduler taskScheduler;
/**
* 新增任务
* @param taskName
* @param cron
* @param runnable
*/
public Boolean addCronTask(String taskName,String cron,Runnable runnable){
if(scheduledTaskMap.containsKey(taskName)){
log.error("动态任务["+ taskName+"]已存在,添加失败");
return Boolean.FALSE;
}
CronTask cronTask = new CronTask(runnable,cron);
ScheduledTask scheduledTask = this.scheduleCronTask(cronTask);
scheduledTaskMap.put(taskName,scheduledTask);
log.info("动态任务["+taskName+"]新增成功");
log.info("剩余动态任务:"+scheduledTaskMap.keySet().stream().collect(Collectors.joining(",")));
return Boolean.TRUE;
}
/**
* 修改动态任务
* @param taskName
* @param cron
*/
public void updateTask(String taskName,String cron,Runnable runnable) {
cancelCronTask(taskName);
addCronTask(taskName, cron,runnable);
}
/**
* 删除任务
* @param taskName
*/
public void cancelCronTask(String taskName){
ScheduledTask scheduledTask = scheduledTaskMap.get(taskName);
if(null != scheduledTask){
scheduledTask.cancel();
scheduledTaskMap.remove(taskName);
}
log.info("动态任务["+taskName+"]删除成功");
log.info("剩余动态任务:"+scheduledTaskMap.keySet().stream().collect(Collectors.joining(", ")));
}
@Override
public void destroy() {
super.destroy();
scheduledTaskMap.values().forEach(ScheduledTask::cancel);
}
}
3.根据每个固定任务逻辑,写出执行类,调用定时任务管理方法即可
@Service
@Slf4j
public class ProbeScheduledTaskManager {
@Autowired
DynamicScheduledTaskRegistrar dynamicScheduledTaskRegistrar;
/**
* 新增任务
*/
public void add(String taskName,String cron){
Boolean result = dynamicScheduledTaskRegistrar.addCronTask(taskName,cronTime,() -> runTask(taskName));
log.info("动态任务添加结果:" + result+","+taskName);
}
/**
* 更新任务
*/
public void update(String taskName,String cron){
cancel(taskName);
add(taskName,cron);
}
/**
* 取消任务
* @param taskName
*/
public void cancel(String taskName){
dynamicScheduledTaskRegistrar.cancelCronTask(taskName);
}
private void runTask(String taskName){
//任务处理逻辑。。。
log.info(taskName+"任务执行结束");
}
}