springboot定时任务及线程池

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

1、启动类

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

1

2

3

4

5

6

7

8

9

10

11

12

13

@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注解声明定时任务,配置时间周期

1

2

3

4

5

6

7

8

9

10

11

@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开启对异步的支持

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@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,这个异步执行器每次都会开启一个子线程执行,性能消耗比较大,所以最好是自己配置线程池

1

2

3

4

5

6

@Async

@Scheduled(cron = "0/1 * * * * ?")

public void method1() {

 logger.info("——————————method1 start——————————");

 logger.info("——————————method1 end——————————");

}

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

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

1、配置多个线程池

分别配置两个线程池

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

@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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

// 间隔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")显示指定。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值