您好,登錄后才能下訂單哦!
這篇文章主要講解了SpringBoot如何配置和使用Schedule,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。
我們在平常項目開發(fā)中,經(jīng)常會用到周期性定時任務(wù),這個時候使用定時任務(wù)就能很方便的實現(xiàn)。在SpringBoot中用得最多的就是Schedule。
一、SpringBoot集成Schedule
1、依賴配置
由于Schedule就包含在spring-boot-starter中,所以無需引入其他依賴。
2、啟用定時任務(wù)
在啟動類或者配置類上增加@EnableScheduling注解。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
3、添加定時任務(wù)
Schdule支持cron表達(dá)式、固定間隔時間、固定頻率三種調(diào)度方式。
1)cron表達(dá)式定時任務(wù)
與Linux下定時任務(wù)用到的Cron表達(dá)式一樣。
字段 | 允許值 | 允許的特殊字符 |
秒(Seconds) | 0~59的整數(shù) | , - * / 四個字符 |
分(Minutes) | 0~59的整數(shù) | , - * / 四個字符 |
小時(Hours) | 0~23的整數(shù) | , - * / 四個字符 |
日期(DayofMonth) | 1~31的整數(shù)(但是你需要考慮該月的天數(shù)) | ,- * ? / L W C 八個字符 |
月份(Month) | 1~12的整數(shù)或者 JAN-DEC | , - * / 四個字符 |
星期(DayofWeek) | 1~7的整數(shù)或者 SUN-SAT (1=SUN) | , - * ? / L C # 八個字符 |
年(可選,留空)(Year) | 1970~2099 | , - * / 四個字符 |
@Component @EnableScheduling public class MyCronTask { private static final Logger logger = LoggerFactory.getLogger(MyCronTask.class); @Scheduled(cron = "0/1 * * * * *") void cronSchedule(){ logger.info("cron schedule execute"); } }
PS:Cron表達(dá)式方式配置的定時任務(wù)如果其執(zhí)行時間超過調(diào)度頻率時,調(diào)度器會在下個執(zhí)行周期執(zhí)行。如第一次執(zhí)行從第0秒開始,執(zhí)行時長3秒,則下次執(zhí)行為第4秒。
2)固定間隔定時任務(wù)
下一次的任務(wù)執(zhí)行時間是從上一次定時任務(wù)結(jié)束時間開始計算。
@Scheduled(fixedDelay = 2) void fixedDelaySchedule() throws Exception{ Thread.sleep(2000); logger.info("fixed delay schedule execute"); }
輸出:
2020-04-23 23:11:54.362 INFO 85325 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed delay schedule execute
2020-04-23 23:11:58.365 INFO 85325 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed delay schedule execute
2020-04-23 23:12:02.372 INFO 85325 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed delay schedule execute
2020-04-23 23:12:06.381 INFO 85325 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed delay schedule execute
3)固定頻率定時任務(wù)
按照指定頻率執(zhí)行任務(wù)
@Scheduled(fixedRate = 2000) void fixedRateSchedule() throws Exception{ Thread.sleep(3000); logger.info("fixed rate schedule execute"); }
輸出:
2020-04-23 23:16:14.750 INFO 85328 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed rate schedule execute
2020-04-23 23:16:17.754 INFO 85328 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed rate schedule execute
2020-04-23 23:16:20.760 INFO 85328 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed rate schedule execute
2020-04-23 23:16:23.760 INFO 85328 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed rate schedule execute
2020-04-23 23:16:26.764 INFO 85328 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed rate schedule execute
PS:當(dāng)方法的執(zhí)行時間超過任務(wù)調(diào)度頻率時,調(diào)度器會在當(dāng)前方法執(zhí)行完成后立即執(zhí)行下次任務(wù)。
二、配置多個定時任務(wù)并發(fā)執(zhí)行
1、并行or串行?
缺省狀態(tài)下,當(dāng)我們沒有給定時任務(wù)配置線程池時,Schedule是串行執(zhí)行,如下:
@Component @EnableScheduling public class MyCronTask { private static final Logger logger = LoggerFactory.getLogger(MyCronTask.class); @Scheduled(fixedDelay = 2000) void task1Schedule() throws Exception{ Thread.sleep(2000); logger.info("task1 execute"); } @Scheduled(fixedDelay = 2000) void task2Schedule() throws Exception{ Thread.sleep(2000); logger.info("task2 execute"); } @Scheduled(fixedDelay = 2000) void task3Schedule() throws Exception{ Thread.sleep(2000); logger.info("task3 execute"); } }
輸出:
2020-04-23 23:19:46.970 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:19:48.973 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:19:50.974 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task3 execute
2020-04-23 23:19:52.978 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:19:54.984 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:19:56.984 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task3 execute
可以看出來只有一個線程穿行執(zhí)行所有定時任務(wù)。
2、Schedule并行執(zhí)行配置
定時調(diào)度的并行化,有兩種配置方式:
1)修改任務(wù)調(diào)度器默認(rèn)使用的線程池:添加一個configuration,實現(xiàn)SchedulingConfigurer接口就可以了。
@Configuration public class ScheduleConfig implements SchedulingConfigurer{ @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.setTaskScheduler(getTaskScheduler()); } @Bean public TaskScheduler getTaskScheduler() { ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); taskScheduler.setPoolSize(3); taskScheduler.setThreadNamePrefix("myworker-"); taskScheduler.setWaitForTasksToCompleteOnShutdown(true); return taskScheduler; } }
再次執(zhí)行后,輸出:
2020-04-23 23:33:14.197 INFO 85461 --- [ myworker-2] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:33:14.197 INFO 85461 --- [ myworker-1] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:33:14.197 INFO 85461 --- [ myworker-3] com.springboot.study.tasks.MyCronTask : task3 execute
2020-04-23 23:33:18.203 INFO 85461 --- [ myworker-2] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:33:18.203 INFO 85461 --- [ myworker-3] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:33:18.204 INFO 85461 --- [ myworker-1] com.springboot.study.tasks.MyCronTask : task3 execute
2020-04-23 23:33:22.208 INFO 85461 --- [ myworker-1] com.springboot.study.tasks.MyCronTask : task3 execute
2020-04-23 23:33:22.208 INFO 85461 --- [ myworker-2] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:33:22.208 INFO 85461 --- [ myworker-3] com.springboot.study.tasks.MyCronTask : task1 execute
2)直接將任務(wù)交給一步線程池處理:啟用@EnableAsync注解,并在每一個定時任務(wù)方法上使用@Async注解。
@Component @EnableScheduling @EnableAsync @Async public class MyCronTask { private static final Logger logger = LoggerFactory.getLogger(MyCronTask.class); @Scheduled(fixedDelay = 2000) void task1Schedule() throws Exception{ Thread.sleep(2000); logger.info("task1 execute"); } @Scheduled(fixedDelay = 2000) void task2Schedule() throws Exception{ Thread.sleep(2000); logger.info("task2 execute"); } @Scheduled(fixedDelay = 2000) void task3Schedule() throws Exception{ Thread.sleep(2000); logger.info("task3 execute"); } }
輸出如下:
2020-04-23 23:38:00.614 INFO 85468 --- [ task-1] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:38:00.614 INFO 85468 --- [ task-3] com.springboot.study.tasks.MyCronTask : task3 execute
2020-04-23 23:38:00.614 INFO 85468 --- [ task-2] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:38:02.620 INFO 85468 --- [ task-4] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:38:02.620 INFO 85468 --- [ task-5] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:38:02.620 INFO 85468 --- [ task-6] com.springboot.study.tasks.MyCronTask : task3 execute
有上面輸出可以看出來這種方式對于每一次定時任務(wù)的執(zhí)行都會創(chuàng)建新的線程,這樣對內(nèi)存資源是一種浪費,嚴(yán)重情況下還會導(dǎo)致服務(wù)掛掉,因此為了更好控制線程的使用,我們可以自定義線程池。
首先配置線程池:
@Configuration public class MyTaskExecutor { @Bean(name = "myExecutor") public TaskExecutor getMyExecutor() { ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(3); taskExecutor.setMaxPoolSize(10); taskExecutor.setQueueCapacity(20); taskExecutor.setThreadNamePrefix("myExecutor-"); taskExecutor.initialize(); return taskExecutor; } }
使用我們自己的線程池:
@Component @EnableScheduling @EnableAsync @Async("myExecutor") public class MyCronTask { private static final Logger logger = LoggerFactory.getLogger(MyCronTask.class); @Scheduled(fixedDelay = 2000) void task1Schedule() throws Exception{ Thread.sleep(2000); logger.info("task1 execute"); } @Scheduled(fixedDelay = 2000) void task2Schedule() throws Exception{ Thread.sleep(2000); logger.info("task2 execute"); } @Scheduled(fixedDelay = 2000) void task3Schedule() throws Exception{ Thread.sleep(2000); logger.info("task3 execute"); } }
輸出:
2020-04-23 23:46:47.404 INFO 85488 --- [ myExecutor-1] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:46:47.404 INFO 85488 --- [ myExecutor-3] com.springboot.study.tasks.MyCronTask : task3 execute
2020-04-23 23:46:47.404 INFO 85488 --- [ myExecutor-2] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:46:49.404 INFO 85488 --- [ myExecutor-3] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:46:49.404 INFO 85488 --- [ myExecutor-2] com.springboot.study.tasks.MyCronTask : task3 execute
2020-04-23 23:46:49.404 INFO 85488 --- [ myExecutor-1] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:46:51.405 INFO 85488 --- [ myExecutor-2] com.springboot.study.tasks.MyCronTask : task2 execute
2020-04-23 23:46:51.405 INFO 85488 --- [ myExecutor-3] com.springboot.study.tasks.MyCronTask : task1 execute
2020-04-23 23:46:51.405 INFO 85488 --- [ myExecutor-1] com.springboot.study.tasks.MyCronTask : task3 execute
看完上述內(nèi)容,是不是對SpringBoot如何配置和使用Schedule有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。