Spring Boot中配置定時(shí)任務(wù)、線程池與多線程池執(zhí)行的方法
配置基礎(chǔ)的定時(shí)任務(wù)
最基本的配置方法,而且這樣配置定時(shí)任務(wù)是單線程串行執(zhí)行的,也就是說每次只能有一個定時(shí)任務(wù)可以執(zhí)行,可以試著聲明兩個方法,在方法內(nèi)寫一個死循環(huán),會發(fā)現(xiàn)一直卡在一個任務(wù)上不動,另一個也沒有執(zhí)行。
1、啟動類
添加@EnableScheduling開啟對定時(shí)任務(wù)的支持
@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、配置執(zhí)行定時(shí)任務(wù)的類
添加@Component掃描本類,在方法上添加@Scheduled注解聲明定時(shí)任務(wù),配置時(shí)間周期
@Component public class TestTask1 { private static final Logger logger = LogManager.getLogger(); // 間隔1秒執(zhí)行一次 @Scheduled(cron = "0/1 * * * * ?") public void method1() { logger.info("——————————method1 start——————————"); logger.info("——————————method1 end——————————"); } }
配置線程池執(zhí)行定時(shí)任務(wù)
因?yàn)橛袝r(shí)候需要執(zhí)行的定時(shí)任務(wù)會很多,如果是串行執(zhí)行會帶來一些問題,比如一個很耗時(shí)的任務(wù)阻塞住了,一些需要短周期循環(huán)執(zhí)行的任務(wù)也會卡住,所以可以配置一個線程池來并行執(zhí)行定時(shí)任務(wù)
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、配置定時(shí)任務(wù)異步執(zhí)行
添加@Async注解,表示該定時(shí)任務(wù)是異步執(zhí)行的,因?yàn)樯厦婢€程池配置了名字,所以可以看到打印的日志是該線程池中的線程在執(zhí)行任務(wù),如果沒有配置線程池的話會默認(rèn)使用SimpleAsyncTaskExecutor,這個異步執(zhí)行器每次都會開啟一個子線程執(zhí)行,性能消耗比較大,所以最好是自己配置線程池
@Async @Scheduled(cron = "0/1 * * * * ?") public void method1() { logger.info("——————————method1 start——————————"); logger.info("——————————method1 end——————————"); }
配置多個線程池分別執(zhí)行不同的定時(shí)任務(wù)
因?yàn)橛行┒〞r(shí)任務(wù)是比較重要的,有些則是不太重要的,想把定時(shí)任務(wù)分別放到不同的線程池中,也是可以實(shí)現(xiàn)的。
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、定時(shí)任務(wù)顯示指定調(diào)用線程池
因?yàn)樯厦嬖谂渲妙惱锩娉跏蓟藘蓚€線程池,所以會有兩個線程池分別叫executor1和executor1被生成放到容器中,因?yàn)锧Bean注解生成的對象默認(rèn)就是和方法名相同的名字,而@Async注解是可以指定使用哪個線程池的。這樣就可以在不同的線程池中執(zhí)行不同的定時(shí)任務(wù)了
// 間隔1秒執(zhí)行一次 @Async("executor1") @Scheduled(cron = "0/1 * * * * ?") public void method1() { logger.info("——————————method1 start——————————"); logger.info("——————————method1 end——————————"); } // 間隔1秒執(zhí)行一次 @Scheduled(cron = "0/1 * * * * ?") @Async("executor2") public void method2() { logger.info("——————————method2 start——————————"); logger.info("——————————method2 end——————————"); }
注意:
- 沒有配置自己的線程池時(shí),會默認(rèn)使用SimpleAsyncTaskExecutor。
- 如果項(xiàng)目中只配置了一個線程池,那么不需要顯示指定使用這個線程池,spring也會自動使用用戶配置的線程池,但是如果配置了多個就必須要顯示指定,否則還是會使用默認(rèn)的。
- 如果想要指定使用哪個線程池,可以使用@Async("executor2")顯示指定。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。
相關(guān)文章
詳解Mybatis多參數(shù)傳遞入?yún)⑺姆N處理方式
這篇文章主要介紹了詳解Mybatis多參數(shù)傳遞入?yún)⑺姆N處理方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04SpringBoot+Vue靜態(tài)資源刷新后無法訪問的問題解決方案
這篇文章主要介紹了SpringBoot+Vue靜態(tài)資源刷新后無法訪問的問題解決方案,文中通過代碼示例和圖文講解的非常詳細(xì),對大家解決問題有一定的幫助,需要的朋友可以參考下2024-05-05java實(shí)現(xiàn)表格tr拖動的實(shí)例(分享)
下面小編就為大家分享一篇java實(shí)現(xiàn)表格tr拖動的實(shí)例。具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12Java中checkbox實(shí)現(xiàn)跨頁多選的方法
最近做了一個項(xiàng)目其中遇到這樣的需求,要實(shí)現(xiàn)checkbox跨頁多選功能,經(jīng)過小編整理,順利解決,今天小編給大家分享Java中checkbox實(shí)現(xiàn)跨頁多選的方法,需要的的朋友參考下2017-01-01Spring HandlerInterceptor實(shí)現(xiàn)原理代碼解析
這篇文章主要介紹了Spring HandlerInterceptor實(shí)現(xiàn)原理代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10