@[toc]
设置 Spring Boot 的定时任务线程池以优雅退出
1、设置线程池
@Component
public class SchedulingPoolConfig {
@Bean
TaskScheduler taskScheduler(){
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(16);
scheduler.setThreadNamePrefix("sc");
scheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
scheduler.setWaitForTasksToCompleteOnShutdown(true);
return scheduler;
}
}
2、置入线程池
@Component
public class SchedulingConfig implements SchedulingConfigurer {
@Autowired
private TaskScheduler taskScheduler;
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.setTaskScheduler(taskScheduler);
}
}