定时任务丢失问题时由于任务调度默认时一个线程执行scheduled-1
1.到点就执行,不管上次有没有执行完成
@EnableAsync
@Bean("threadPool")
public Executor threadPool() {
return Executors.newScheduledThreadPool(5);
}
@Scheduled(cron = "0/1 * * * * *")
@Async("threadPool") // 到点就执行,不管上次有没有执行
public void jobTwo() throws InterruptedException {
System.out.println(String.format("%-5s %-6s %-23s %s", "2", "start", LocalDateTime.now(), Thread.currentThread().getName()));
Thread.sleep(1500);
System.out.println(String.format("%-5s %-6s %-23s %s", "2", "end", LocalDateTime.now(), Thread.currentThread().getName()));
}
2.如果当前有任务正在执行,则当期任务不执行
public class ScheduledJob implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(threadPool()); // 如果当前有任务正在执行,则当期任务不执行
}