1.定义线程池
@Bean("sp-syncScheduler")
public ThreadPoolTaskScheduler syncScheduler() {
ThreadPoolTaskScheduler syncScheduler = new ThreadPoolTaskScheduler();
syncScheduler.setPoolSize(5);
syncScheduler.setThreadGroupName("sp");
syncScheduler.setThreadNamePrefix("spThread-");
syncScheduler.initialize();
return syncScheduler;
}
2.定义任务服务类
@Service
@Slf4j
@RequiredArgsConstructor
public class SpDynamicTaskService {
public Map<String, ScheduledFuture<?>> taskMap = new ConcurrentHashMap<>();
public Map<String, SpTaskDo> taskDoMap = new HashMap<>();
@Resource(name = "sp-syncScheduler")
private ThreadPoolTaskScheduler syncScheduler;
private final String IDENTIFY_TASK_TIME = "0/5 * * * * ?";
private final String TASK_NAME = "任务名称";
private final String CAPTURE_TASK_TIME = "0 30 8 * * ? ";
public SpTaskDto getTask() {
Set<Map.Entry<String, SitePlotTaskDo>> set = taskDoMap.entrySet();
SpTaskDto taskDto = new SpTaskDto();
for (Map.Entry<String, SpTaskDto> stringSpTaskDoEntry : set) {
taskDto.setTaskName(stringSitePlotTaskDoEntry.getValue().getTaskName());
taskDto.setCron(stringSitePlotTaskDoEntry.getValue().getCron());
taskDto.setLastExecutionTime(stringSitePlotTaskDoEntry.getValue().getLastExecutionTime());
taskDto.setNextExecutionTime(stringSitePlotTaskDoEntry.getValue().getNextExecutionTime());
return taskDto;
}
return taskDto;
}
@PostConstruct
public void taskInit(){
log.info("===================定时任务初始化===================");
SpTaskDo spTaskDo = new SpTaskDo();
spTaskDo.setTaskName(TASK_NAME);
spTaskDo.setCron(CAPTURE_TASK_TIME);
this.add(spTaskDo);
}
public Response add(SpTaskDo task) {
if (null != taskMap.get(task.getTaskName())) {
stop(task.getTaskName());
}
ScheduledFuture<?> scheduleCapture = syncScheduler.schedule(getCaptureRunnable(task), new CronTrigger(task.getCron()));
taskMap.put(task.getTaskName(), scheduleCapture);
Date date = DateUtils.addSeconds(new Date(), Long.valueOf(scheduleCapture.getDelay(TimeUnit.SECONDS)).intValue());
task.setLastExecutionTime(DateUtil.formatDateTime(date));
task.setNextExecutionTime(task.getLastExecutionTime());
taskDoMap.put(task.getTaskName(), task);
return Response.buildSuccess();
}
public Runnable getCaptureRunnable(SpTaskDo task) {
return () -> {
log.info("---任务运行开始---");
task.setLastExecutionTime(task.getNextExecutionTime());
LocalDateTime nextFirst = CronExpression.parse(task.getCron()).next(LocalDateTime.now());
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
task.setNextExecutionTime(dtf2.format(nextFirst));
taskDoMap.put(task.getTaskName(), task);
log.info("---任务运行结束---");
};
}
public Response stop(String name) {
if (null == taskMap.get(name)) {
return Response.buildFailure("500", "任务不存在");
}
ScheduledFuture<?> scheduledFuture = taskMap.get(name);
scheduledFuture.cancel(true);
taskMap.remove(name);
taskDoMap.remove(name);
return Response.buildSuccess();
}
3.任务实体
@Data
public class SpTaskDo {
private String taskName;
private String cron;
private String lastExecutionTime;
private String nextExecutionTime;
}