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


    配置基础的定时任务

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

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

    总结

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

     转自:http://www.3qphp.com/java/framework/3131.html
  • 相关阅读:
    tomcat日志切割脚本
    开源项目推荐:GSL科学计算函数库(GNU Scientific Library),实现VS2019源码编译
    MCUXpresso环境开发(本文争取做到您能够快速简易应用入门,知道大体流程,复杂功能和窗口设置一律不管)
    Arduino uno r3 使用 ESP8266 UARTWiFi 透传模块
    ESP8266引脚的说明
    windows10安装Trading View出错解决办法
    windows or linux 64位安装talib包
    python中list数组指定类型
    面试官问我JVM调优,我忍不住了!
    稳了!我准备了1个晚上的CMS垃圾收集器
  • 原文地址:https://www.cnblogs.com/javalinux/p/16363330.html
Copyright © 2020-2023  润新知