SpringBoot中定時任務的使用方法解析
1.定時任務注解
在springboot啟動器開啟定時任務注解
這里的 @EnableScheduling 注解,它的作用是發(fā)現(xiàn)注解 @Scheduled的任務并由后臺執(zhí)行。沒有它的話將無法執(zhí)行定時任務。
2.使用的@Scheduled 注解
2.1 @Scheduled 的常用配置項
@Scheduled(fixedRate=2000):上一次開始執(zhí)行時間點后2秒再次執(zhí)行,無論上一個任務是否執(zhí)行完過2s就開始下一個任務;
@Scheduled(fixedDelay=2000):上一次執(zhí)行完畢時間點后2秒再次執(zhí)行,上一個任務執(zhí)行完后過2s 才執(zhí)行下個任務;
@Scheduled(initialDelay=1000, fixedDelay=2000):第一次延遲1秒執(zhí)行, 然后在上一次執(zhí)行完畢時間點后2秒再次執(zhí)行;
@Scheduled(cron=“* * * * * ?”):按cron規(guī)則執(zhí)行。 在線Cron表達式生成器://cron.qqe2.com/介紹
3.實戰(zhàn)
3.1 @Scheduled 多線程并發(fā)執(zhí)行
@Scheduled 默認是使用單線程執(zhí)行所有任務的。
1)驗證:
編寫多個@Scheduled 任務且設置執(zhí)行時間和時間間隔都相同。
@Component public class AnnotationSchedulTask { private static final Logger LOG = LoggerFactory.getLogger(AnnotationSchedulTask.class); private int count=0; @Scheduled(cron = "0/2 * * * * ? ") public void test(){ //0/1 第一次運行間隔0s,每次間隔1s運行一次 LOG.info("SchedulTask run count:"+count++); } /** * 測試@Scheduled 默認是否是單線程運行 */ @Scheduled(cron = "0/2 * * * * ? ") public void testConcurrence(){ LOG.info("*********testConcurrence run**************"); } /** * 測試@Scheduled 默認是否是單線程運行 */ @Scheduled(cron = "0/2 * * * * ? ") public void testConcurrence2(){ LOG.info("--------testConcurrence2-----------"); } }
啟動服務運行結果:
可以看到所有任務都被 scheduling-1 線程執(zhí)行
原因:
從ScheduledAnnotationBeanPostProcessor類開始一路找下去,在ScheduledTaskRegistrar(定時任務注冊類)中的ScheduleTasks中又這樣一段判斷:
if (this.taskScheduler == null) { this.localExecutor = Executors.newSingleThreadScheduledExecutor(); this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor); }
當this.taskScheduler 為null 時 Executors.newSingleThreadScheduledExecutor(); 單個線程的線程池。
2)擴展多線程
其實只要設置一個我們自定義的this.taskScheduler
方法一: 配置一個ScheduledExecutorService bean即可設置
/** * 執(zhí)行周期性或定時任務 */ @Bean(name = "scheduledExecutorService") protected ScheduledExecutorService scheduledExecutorService() { return new ScheduledThreadPoolExecutor(corePoolSize, new BasicThreadFactory.Builder().namingPattern("MySchedule-pool-%d").daemon(true).build(), new ThreadPoolExecutor.CallerRunsPolicy()) { @Override protected void afterExecute(Runnable r, Throwable t) { super.afterExecute(r, t); if (t == null && r instanceof Future<?>) { try { Future<?> future = (Future<?>) r; if (future.isDone()) { future.get(); } } catch (CancellationException ce) { t = ce; } catch (ExecutionException ee) { t = ee.getCause(); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); } } if (t != null) { log.error(t.getMessage(), t); } } }; }
執(zhí)行結果:
可以看到現(xiàn)在@Scheduled 由不同線程執(zhí)行。
原理:
ScheduledAnnotationBeanPostProcessor 類的finishRegistration方法
最終在 this.registrar.setScheduler(resolveSchedulerBean(this.beanFactory, ScheduledExecutorService.class, false));
這一行 我們自定義的ScheduledExecutorService bean被當作執(zhí)行的線程池。
方法2:
@Configurationpublic class ScheduleConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.setScheduler( new ScheduledThreadPoolExecutor(8, new BasicThreadFactory.Builder().namingPattern("taskRegistrar-pool-%d").daemon(true).build(), new ThreadPoolExecutor.CallerRunsPolicy())); }}
運行結果:
可以看出來也成功了,并且覆蓋了我們之前設置的ScheduledExecutorService。
3.2 cron 表達式實現(xiàn)指定時間執(zhí)行任務
1)cron 表達式詳細介紹:
- 域 必填 允許值 允許的通配符
- 秒(seconds) 是 0-59整數(shù) , - * /
- 分(minutes) 是 0-59整數(shù) , - * /
- 時(hours) 是 0-23整數(shù) , - * /
- 日(daysOfMonth) 是 1-31整數(shù)(需要考慮月的天數(shù)) , - * ? / L W
- 月(months) 是 1-12整數(shù) 或 JAN-DEC , - * /
- 周 (daysOfWeek) 是 1-7整數(shù) 或 SUN-SAT , - * ? / L #
通配符說明:
- *:表示匹配該域的任意值。在minutes域使用 * 表示每分鐘。在months里表示每個月。在daysOfWeek域表示一周的每一天。
- ?:只能用在daysofMonth和daysofWeek兩個域,表示不指定值,當兩個子表達式其中之一被指定了值以后,為了避免沖突,需要將另一個子表達式的值設為 ?。因為daysofMonth和daysofWeek會相互影響。例如想在每月的2號觸發(fā)調度,不管2號是周幾,則只能使用如下寫法:0 0 0 2 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管周幾都會觸發(fā)。
- -:表示范圍。例如在minutes域使用5-20,表示從5分到20分鐘每分鐘觸發(fā)一次
- /:表示起始時間開始觸發(fā),然后每隔固定時間觸發(fā)一次。例如在minutes域使用5/20,則意味著從當前小時的第5分鐘開每20分鐘觸發(fā)一次。
- ,:表示列出枚舉值。例如:在minutes域使用5,20,則意味著在5分和20分時各觸發(fā)一次。
- L:表示最后,是單詞“last”的縮寫,只能出現(xiàn)在daysofWeek和dayofMonth域。在daysofWeek域使用5L意思是在指定月的最后的一個星期四觸發(fā)。在dayofMonth域使用5L或者FRIL意思是在指定月的倒數(shù)第5天觸發(fā)。在使用L參數(shù)時,不要指定列表或范圍。
- W:表示有效工作日(周一到周五),只能出現(xiàn)在daysofMonth域,系統(tǒng)將在離指定日期的最近的有效工作日觸發(fā)事件。例如:在daysofMonth使用5W,如果5號是周六,則將在最近的工作日周五,即4號觸發(fā)。如果5號是周日,則在6日(周一)觸發(fā)。如果5日在星期一到星期五中的一天,則就在5日觸發(fā)。另外,W的最近尋找不會跨過月份 。
- LW:這兩個字符可以連用,表示指定月的最后一個工作日。
- #:用于確定每個月第幾個周幾,只能出現(xiàn)在daysOfWeek域。例如在4#2,表示某月的第二個周三。
常用表達式示例:
- 0/2 * * * * ? 表示每2秒 執(zhí)行任務
- 0 0/2 * * * ? 表示每2分鐘 執(zhí)行任務
- 0 0 2 1 * ? 表示在每月的1日的凌晨2點調整任務
- 0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15執(zhí)行作業(yè)
- 0 0 10,14,16 * * ? 每天上午10點,下午2點,4點
- 0 0/30 9-17 * * ? 朝九晚五工作時間內每半小時
- 0 0 12 ? * WED 表示每個星期三中午12點
- 0 0 12 * * ? 每天中午12點觸發(fā)
- 0 15 10 ? * * 每天上午10:15觸發(fā)
- 0 15 10 * * ? 每天上午10:15觸發(fā)
- 0 15 10 * * ? 每天上午10:15觸發(fā)
- 0 * 14 * * ? 在每天下午2點到下午2:59期間的每1分鐘觸發(fā)
- 0 0/5 14 * * ? 在每天下午2點到下午2:55期間的每5分鐘觸發(fā)
- 0 0/5 14,18 * * ? 在每天下午2點到2:55期間和下午6點到6:55期間的每5分鐘觸發(fā)
- 0 0-5 14 * * ? 在每天下午2點到下午2:05期間的每1分鐘觸發(fā)
- 0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44觸發(fā)
- 0 15 10 ? * MON-WED,SAT 周一至周三和周六的上午10:15觸發(fā)
- 0 15 10 15 * ? 每月15日上午10:15觸發(fā)
- 0 15 10 L * ? 每月最后一日的上午10:15觸發(fā)
- 0 15 10 ? * 6L 每月的最后一個星期五上午10:15觸發(fā)
- 0 15 10 ? * 6#3 每月的第三個星期五上午10:15觸發(fā)
2)設置每天固定時間執(zhí)行
//測試每天下午兩點執(zhí)行 @Scheduled(cron = "0 0 14 * * ? ") public void testCron2(){ LOG.info("*********testCron2 run**************"); }
結果:
在下午兩點被執(zhí)行
14:00:00.005 [taskRegistrar-pool-1] INFO c.e.s.api.task.AnnotationSchedulTask
*********testCron2 run**************
3)設定沒1分鐘執(zhí)行一次
//測試每分鐘執(zhí)行一次 @Scheduled(cron = "0 0/1 * * * ? ") public void testFixedDelay(){ LOG.info("*********testFixedDelay run**************"); }
結果:
每分鐘執(zhí)行一次
4)設定每月幾號執(zhí)行一次
必須指定執(zhí)行時間不然當天每分鐘都會被執(zhí)行一次。
//設定每月幾號執(zhí)行一次 @Scheduled(cron = "0 14 14 3 * ? ") public void testFixedDay(){ LOG.info("*********testFixedDay run**************"); }
結果:
在 3號的 14.14 被執(zhí)行。
到此這篇關于SpringBoot中定時任務的使用方法解析的文章就介紹到這了,更多相關SpringBoot定時任務內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
手工體驗smtp和pop3協(xié)議 郵件實現(xiàn)詳解(二)
POP3/IMAP協(xié)議定義了郵件客戶端軟件和POP3郵件服務器的通信規(guī)則,這篇文章我們就來手工體驗SMTP和POP3協(xié)議的奧秘,感興趣的小伙伴們可以參考一下2017-10-10初識Spring Boot框架之Spring Boot的自動配置
本篇文章主要介紹了初識Spring Boot框架之Spring Boot的自動配置,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-04-04SpringBoot中TransactionTemplate事務管理的實現(xiàn)
Spring Boot提供了多種方式來管理事務,其中之一是使用TransactionTemplate,本文主要介紹了SpringBoot中TransactionTemplate事務管理的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-04-04Java日期時間處理問題(從Date、Calendar到SimpleDateFormat)
這篇文章主要介紹了Java日期時間處理深度解析(從Date、Calendar到SimpleDateFormat),我們詳細討論了Java中的日期和時間處理,包括Date、Calendar和SimpleDateFormat類的使用,以及Java?8引入的新的日期時間API的優(yōu)勢,需要的朋友可以參考下2024-08-08