当前位置: 移动技术网 > IT编程>开发语言>Java > Spring Boot中配置定时任务、线程池与多线程池执行的方法

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

2019年09月09日  | 移动技术网IT编程  | 我要评论

配置基础的定时任务

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

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

总结

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网