Spring @Scheduled限制
@Scheduled具有一定的限制性,它畢竟不是quartz,只是簡單的定時,比jdk Timer就加入了線程池而以
-
@Scheduled
不支持年份定時 -
@Scheduled
不支持W L這些字母
沒辦法 如果非要使用那就只能放棄注解使用XML方式了
Spring多定時任務@Scheduled執行阻塞
一. 問題描述
最近項目中發現一個問題,計劃每日凌晨4:40執行一個定時任務,使用注解方式: @Scheduled(cron = “0 40 4 * * ?”),cron表達式明顯沒有問題,但是這個定時任務總是不按時執行,有時候得等到8點多,有時候9點多才執行。后來查了下,原來這種定時方式默認是單線程執行的,恰好我這里有多個定時任務,并且其中有個在4:40之前的定時任務比較耗時,導致4:40的任務只能等待之前的任務執行完成才能夠觸發,所以要自己手動把定時任務設置成多線程的方式才行。
二. 場景復現
項目描述:使用Springboot進行開發
設置兩個定時任務,每5s執行一次,并打印出其執行情況
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@Component @Log4j2 public class ScheduledTask { @Scheduled (cron = "0/5 * * * * ?" ) public void task1() throws InterruptedException { log.info( "I am task11111111, current thread: {}" , Thread.currentThread()); while ( true ) { //模擬耗時任務,阻塞10s Thread.sleep( 10000 ); break ; } } @Scheduled (cron = "0/5 * * * * ?" ) public void task2() { log.info( "I am task22222222, current thread: {}" , Thread.currentThread()); } } |
執行結果如下:
2019-04-24 17:11:15.008 INFO 16868 --- [ scheduling-1] com.example.demo.task.ScheduledTask : I am task22222222, current thread: Thread[scheduling-1,5,main]
2019-04-24 17:11:15.009 INFO 16868 --- [ scheduling-1] com.example.demo.task.ScheduledTask : I am task11111111, current thread: Thread[scheduling-1,5,main]
2019-04-24 17:11:25.009 INFO 16868 --- [ scheduling-1] com.example.demo.task.ScheduledTask : I am task22222222, current thread: Thread[scheduling-1,5,main]
2019-04-24 17:11:30.002 INFO 16868 --- [ scheduling-1] com.example.demo.task.ScheduledTask : I am task22222222, current thread: Thread[scheduling-1,5,main]
2019-04-24 17:11:30.003 INFO 16868 --- [ scheduling-1] com.example.demo.task.ScheduledTask : I am task11111111, current thread: Thread[scheduling-1,5,main]
2019-04-24 17:11:40.004 INFO 16868 --- [ scheduling-1] com.example.demo.task.ScheduledTask : I am task22222222, current thread: Thread[scheduling-1,5,main]
由結果可見,task1與task2由同一個線程Thread[scheduling-1,5,main]執行,也即該定時任務默認使用單線程,并且由于task1阻塞了10s,導致本應5s執行一次的定時任務10s才執行一次。
三. 解決方案
網上有多種解決方案,以下列舉兩種
方案一:使用@Async注解實現異步任務
這種方式比較簡單,在定時任務上加上@Async注解,注意:需啟動類配合加上 @EnableAsync才會生效
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@Component @Log4j2 public class ScheduledTask { @Async @Scheduled (cron = "0/5 * * * * ?" ) public void task1() throws InterruptedException { log.info( "I am task11111111, current thread: {}" , Thread.currentThread()); while ( true ) { //模擬耗時任務,阻塞10s Thread.sleep( 10000 ); break ; } } @Async @Scheduled (cron = "0/5 * * * * ?" ) public void task2() { log.info( "I am task22222222, current thread: {}" , Thread.currentThread()); } } |
運行結果:
2019-04-24 17:03:00.024 INFO 2152 --- [ task-1] com.example.demo.task.ScheduledTask : I am task22222222, current thread: Thread[task-1,5,main]
2019-04-24 17:03:00.024 INFO 2152 --- [ task-2] com.example.demo.task.ScheduledTask : I am task11111111, current thread: Thread[task-2,5,main]
2019-04-24 17:03:05.001 INFO 2152 --- [ task-3] com.example.demo.task.ScheduledTask : I am task11111111, current thread: Thread[task-3,5,main]
2019-04-24 17:03:05.001 INFO 2152 --- [ task-4] com.example.demo.task.ScheduledTask : I am task22222222, current thread: Thread[task-4,5,main]
2019-04-24 17:03:10.002 INFO 2152 --- [ task-5] com.example.demo.task.ScheduledTask : I am task22222222, current thread: Thread[task-5,5,main]
2019-04-24 17:03:10.003 INFO 2152 --- [ task-6] com.example.demo.task.ScheduledTask : I am task11111111, current thread: Thread[task-6,5,main]
由運行日志可見,定時每5s執行一次已生效,且每次任務使用的線程不一樣,也即實現了多線程執行定時任務,不會出現任務等待現象。此方式據說默認線程池大小為100,要是任務不多的話有點大材小用了,所以我覺得第二種方式比較好。
方案二:手動設置定時任務的線程池大小
定時任務代碼部分還原,不使用@Async注解,新增啟動代碼配置:
1
2
3
4
5
6
7
8
9
10
11
12
|
@Configuration public class AppConfig implements SchedulingConfigurer { @Bean public Executor taskExecutor() { //指定定時任務線程數量,可根據需求自行調節 return Executors.newScheduledThreadPool( 3 ); } @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { scheduledTaskRegistrar.setScheduler(taskExecutor()); } } |
運行結果如下:
2019-04-24 17:26:15.008 INFO 2164 --- [pool-1-thread-2] com.example.demo.task.ScheduledTask : I am task22222222, current thread: Thread[pool-1-thread-2,5,main]
2019-04-24 17:26:15.008 INFO 2164 --- [pool-1-thread-1] com.example.demo.task.ScheduledTask : I am task11111111, current thread: Thread[pool-1-thread-1,5,main]
2019-04-24 17:26:20.002 INFO 2164 --- [pool-1-thread-2] com.example.demo.task.ScheduledTask : I am task22222222, current thread: Thread[pool-1-thread-2,5,main]
2019-04-24 17:26:25.001 INFO 2164 --- [pool-1-thread-2] com.example.demo.task.ScheduledTask : I am task22222222, current thread: Thread[pool-1-thread-2,5,main]
2019-04-24 17:26:30.001 INFO 2164 --- [pool-1-thread-1] com.example.demo.task.ScheduledTask : I am task11111111, current thread: Thread[pool-1-thread-1,5,main]
2019-04-24 17:26:30.001 INFO 2164 --- [pool-1-thread-3] com.example.demo.task.ScheduledTask : I am task22222222, current thread: Thread[pool-1-thread-3,5,main]
2019-04-24 17:26:35.001 INFO 2164 --- [pool-1-thread-3] com.example.demo.task.ScheduledTask : I am task22222222, current thread: Thread[pool-1-thread-3,5,main]
由結果可見,第二種方式也實現了多線程任務調度。
四. 總結
兩種方式各有優缺點:
比較 | 方案一 | 方案二 |
---|---|---|
優點 | 注解方式使用簡單,代碼量少 | 配置靈活,線程數可控 |
缺點 | 線程數不可控,可能存在資源浪費 | 需要增加編碼 |
留個坑,從日志上看@Async方式針對同一任務也是異步的,也即task1每5s會執行一次,但是方式二貌似對同一個任務不會生效,task1執行的時候需等待上一次執行結束才會觸發,并沒有每5s執行一次。關于這個現象,下次再琢磨…
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/s1040342522/article/details/78275449