@EnableScheduling
就可以/**
* @author liouwb
*/
@SpringBootApplication
@EnableScheduling
public class SchedulerApplication{
public static void main(String[] args) {
SpringApplication.run(SchedulerApplication.class, args);
}
}
/**
* @author liouwb
*/
@Slf4j
@Component
public class TestJob {
/**
* 定时任务-串行
* 固定一秒执行一次
*
* @author liouwb
*/
@Scheduled(cron = "0/1 * * * * ?")
public void testTask1() {
log.info("测试任务-1");
}
}
/**
* 定时任务-串行
* 固定一秒执行一次
*
* @author liouwb
*/
@Scheduled(cron = "0/1 * * * * ?")
public void testTask1() throws InterruptedException {
// 让每次任务执行5秒
Thread.sleep(5 * 1000);
log.info("测试任务-1");
}
@EnableScheduling
注解默认使用的是ThreadPoolTaskScheduler
线程池,默认线程数是1@EnableScheduling
注解@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(SchedulingConfiguration.class)
@Documented
public @interface EnableScheduling {
}
SchedulingConfiguration
类@Configuration
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public class SchedulingConfiguration {
@Bean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public ScheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() {
return new ScheduledAnnotationBeanPostProcessor();
}
}
ScheduledAnnotationBeanPostProcessor
类 public ScheduledAnnotationBeanPostProcessor() {
this.registrar = new ScheduledTaskRegistrar();
}
SchedulingConfigurer
接口,实现configureTasks
方法/**
* 线程池配置
*
* @author liouwb
* @time 2023-07-27
*/
@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskScheduler());
}
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
// 设置线程池数量
taskScheduler.setPoolSize(10);
// 设置线程池前缀
taskScheduler.setThreadNamePrefix("parallelScheduler-");
return taskScheduler;
}
}
@EnableAsync
注解/**
* @author liouwb
*/
@SpringBootApplication
@EnableAsync
@EnableScheduling
public class SchedulerApplication{
public static void main(String[] args) {
SpringApplication.run(SchedulerApplication.class, args);
}
}
@Async
注解便可以让方法异步执行/**
* @author liouwb
*/
@Slf4j
@Component
public class TestJob {
/**
* 定时任务-串行
* 固定一秒执行一次
*
* @author liouwb
*/
@Scheduled(cron = "0/1 * * * * ?")
public void testTask1() throws InterruptedException {
// 让每次任务执行5秒
Thread.sleep(5 * 1000);
log.info("测试任务-1");
}
@Async
@Scheduled(cron = "0/1 * * * * ?")
public void testTask2() {
log.info("测试任务-2");
}
}
springboot
异步线程池默认使用的是ThreadPoolTaskExecutor
ThreadPoolTaskExecutor
和ThreadPoolTaskScheduler
都在org.springframework.scheduling.concurrent
下@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AsyncConfigurationSelector.class)
public @interface EnableAsync {
/**
* 线程池配置
*
* @author liouwb
*/
@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskScheduler());
}
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
// 设置线程池前缀
taskScheduler.setThreadNamePrefix("parallelScheduler-");
return taskScheduler;
}
/**
* 配置异步线程池
*
* @author liouwb
* @rutern org.springframework.core.task.TaskExecutor
*/
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 设置核心线程数
executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
// 设置最大线程数
executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 10);
// 设置队列容量
executor.setQueueCapacity(Runtime.getRuntime().availableProcessors() * 10);
// 设置线程活跃时间(秒)
executor.setKeepAliveSeconds(10);
// 设置默认线程名称
executor.setThreadNamePrefix("ansyScheduled-");
// 设置拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
return executor;
}
}
/**
* 线程池配置
*
* @author liouwb
* @time 2023-07-27
*/
@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskScheduler());
}
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(10);
// 设置线程池前缀
taskScheduler.setThreadNamePrefix("parallelScheduler-");
return taskScheduler;
}
/**
* 配置异步线程池
*
* @author liouwb
* @rutern org.springframework.core.task.TaskExecutor
*/
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 设置核心线程数
executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
// 设置最大线程数
executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 10);
// 设置队列容量
executor.setQueueCapacity(Runtime.getRuntime().availableProcessors() * 10);
// 设置线程活跃时间(秒)
executor.setKeepAliveSeconds(10);
// 设置默认线程名称
executor.setThreadNamePrefix("ansyScheduled-");
// 设置拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
return executor;
}
/**
* 配置异步线程池2
*
* @author liouwb
* @rutern org.springframework.core.task.TaskExecutor
*/
@Bean
public TaskExecutor taskExecutor2() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 设置核心线程数
executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
// 设置最大线程数
executor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 10);
// 设置队列容量
executor.setQueueCapacity(Runtime.getRuntime().availableProcessors() * 10);
// 设置线程活跃时间(秒)
executor.setKeepAliveSeconds(10);
// 设置默认线程名称
executor.setThreadNamePrefix("异步线程池2-");
// 设置拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
return executor;
}
}
/**
* @author liouwb
*/
@Slf4j
@Component
public class TestJob {
/**
* 定时任务-串行
* 固定一秒执行一次
*
* @author liouwb
*/
@Scheduled(cron = "0/1 * * * * ?")
public void testTask1() throws InterruptedException {
// 让每次任务执行5秒
Thread.sleep(5 * 1000);
log.info("测试任务-1");
}
/**
* 异步执行
*
* @author liouwb
*/
@Async
@Scheduled(cron = "0/1 * * * * ?")
public void testTask2() {
log.info("测试任务-2");
}
/**
* 异步执行
* 指定使用taskExecutor2线程池
*
* @author liouwb
*/
@Async(value = "taskExecutor2")
@Scheduled(cron = "0/1 * * * * ?")
public void testTask3() {
log.info("测试任务-指定线程池-3");
}
}