您的位置:首页 > 编程语言 > Java开发

Spring Boot中配置定时任务、线程池与多线程池执行的方法

2019-09-05 16:39 1706 查看

配置基础的定时任务

最基本的配置方法,而且这样配置定时任务是单线程串行执行的,也就是说每次只能有一个定时任务可以执行,可以试着声明两个方法,在方法内写一个死循环,会发现一直卡在一个任务上不动,另一个也没有执行。

1、启动类

添加@EnableScheduling开启对定时任务的支持

@EnableScheduling
@SpringBootApplication
public class TestScheduledApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(this.getClass());
}

public static void main(String[] args) {
new SpringApplicationBuilder(TestScheduledApplication.class).web(true).run(args);
}
}

2、配置执行定时任务的类

添加@Component扫描本类,在方法上添加@Scheduled注解声明定时任务,配置时间周期

@Component
public class TestTask1 {
private static final Logger logger = LogManager.getLogger();

// 间隔1秒执行一次
@Scheduled(cron = "0/1 * * * * ?")
public void method1() {
logger.info("――――――――――method1 start――――――――――");
logger.info("――――――――――method1 end――――――――――");
}
}

配置线程池执行定时任务

因为有时候需要执行的定时任务会很多,如果是串行执行会带来一些问题,比如一个很耗时的任务阻塞住了,一些需要短周期循环执行的任务也会卡住,所以可以配置一个线程池来并行执行定时任务

1、配置线程池

添加@EnableAsync开启对异步的支持

@Configuration
@EnableAsync
public class ExecutorConfig {

@Bean
public Executor executor1() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setThreadNamePrefix("test-schedule2-");
executor.setMaxPoolSize(20);
executor.setCorePoolSize(15);
executor.setQueueCapacity(0);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}

2、配置定时任务异步执行

添加@Async注解,表示该定时任务是异步执行的,因为上面线程池配置了名字,所以可以看到打印的日志是该线程池中的线程在执行任务,如果没有配置线程池的话会默认使用SimpleAsyncTaskExecutor,这个异步执行器每次都会开启一个子线程执行,性能消耗比较大,所以最好是自己配置线程池

@Async
@Scheduled(cron = "0/1 * * * * ?")
public void method1() {
logger.info("――――――――――method1 start――――――――――");
logger.info("――――――――――method1 end――――――――――");
}

配置多个线程池分别执行不同的定时任务

因为有些定时任务是比较重要的,有些则是不太重要的,想把定时任务分别放到不同的线程池中,也是可以实现的。

1、配置多个线程池

分别配置两个线程池

@Configuration
@EnableAsync
public class ExecutorConfig1 {

@Bean
public Executor executor1() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setThreadNamePrefix("test-schedule1-");
executor.setMaxPoolSize(20);
executor.setCorePoolSize(15);
executor.setQueueCapacity(0);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}

@Bean
public Executor executor2() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setThreadNamePrefix("test-schedule2-");
executor.setMaxPoolSize(20);
executor.setCorePoolSize(15);
executor.setQueueCapacity(0);
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}

2、定时任务显示指定调用线程池

因为上面在配置类里面初始化了两个线程池,所以会有两个线程池分别叫executor1和executor1被生成放到容器中,因为@Bean注解生成的对象默认就是和方法名相同的名字,而@Async注解是可以指定使用哪个线程池的。这样就可以在不同的线程池中执行不同的定时任务了

// 间隔1秒执行一次
@Async("executor1")
@Scheduled(cron = "0/1 * * * * ?")
public void method1() {
logger.info("――――――――――method1 start――――――――――");
logger.info("――――――――――method1 end――――――――――");
}

// 间隔1秒执行一次
@Scheduled(cron = "0/1 * * * * ?")
@Async("executor2")
public void method2() {
logger.info("――――――――――method2 start――――――――――");
logger.info("――――――――――method2 end――――――――――");
}

注意:

  1. 没有配置自己的线程池时,会默认使用SimpleAsyncTaskExecutor。
  2. 如果项目中只配置了一个线程池,那么不需要显示指定使用这个线程池,spring也会自动使用用户配置的线程池,但是如果配置了多个就必须要显示指定,否则还是会使用默认的。
  3. 如果想要指定使用哪个线程池,可以使用@Async("executor2")显示指定。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息