這篇文章主要講解了SpringBoot如何配置和使用Schedule,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
創(chuàng)新互聯(lián)建站專(zhuān)注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、成華網(wǎng)絡(luò)推廣、成都小程序開(kāi)發(fā)、成華網(wǎng)絡(luò)營(yíng)銷(xiāo)、成華企業(yè)策劃、成華品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供成華建站搭建服務(wù),24小時(shí)服務(wù)熱線:18982081108,官方網(wǎng)址:aaarwkj.com
我們?cè)谄匠m?xiàng)目開(kāi)發(fā)中,經(jīng)常會(huì)用到周期性定時(shí)任務(wù),這個(gè)時(shí)候使用定時(shí)任務(wù)就能很方便的實(shí)現(xiàn)。在SpringBoot中用得最多的就是Schedule。
一、SpringBoot集成Schedule
1、依賴(lài)配置
由于Schedule就包含在spring-boot-starter中,所以無(wú)需引入其他依賴(lài)。
2、啟用定時(shí)任務(wù)
在啟動(dòng)類(lèi)或者配置類(lèi)上增加@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、添加定時(shí)任務(wù)
Schdule支持cron表達(dá)式、固定間隔時(shí)間、固定頻率三種調(diào)度方式。
1)cron表達(dá)式定時(shí)任務(wù)
與Linux下定時(shí)任務(wù)用到的Cron表達(dá)式一樣。
字段 | 允許值 | 允許的特殊字符 |
秒(Seconds) | 0~59的整數(shù) | , - * / 四個(gè)字符 |
分(Minutes) | 0~59的整數(shù) | , - * / 四個(gè)字符 |
小時(shí)(Hours) | 0~23的整數(shù) | , - * / 四個(gè)字符 |
日期(DayofMonth) | 1~31的整數(shù)(但是你需要考慮該月的天數(shù)) | ,- * ? / L W C 八個(gè)字符 |
月份(Month) | 1~12的整數(shù)或者 JAN-DEC | , - * / 四個(gè)字符 |
星期(DayofWeek) | 1~7的整數(shù)或者 SUN-SAT (1=SUN) | , - * ? / L C # 八個(gè)字符 |
年(可選,留空)(Year) | 1970~2099 | , - * / 四個(gè)字符 |
@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á)式方式配置的定時(shí)任務(wù)如果其執(zhí)行時(shí)間超過(guò)調(diào)度頻率時(shí),調(diào)度器會(huì)在下個(gè)執(zhí)行周期執(zhí)行。如第一次執(zhí)行從第0秒開(kāi)始,執(zhí)行時(shí)長(zhǎng)3秒,則下次執(zhí)行為第4秒。
2)固定間隔定時(shí)任務(wù)
下一次的任務(wù)執(zhí)行時(shí)間是從上一次定時(shí)任務(wù)結(jié)束時(shí)間開(kāi)始計(jì)算。
@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)固定頻率定時(shí)任務(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í)行時(shí)間超過(guò)任務(wù)調(diào)度頻率時(shí),調(diào)度器會(huì)在當(dāng)前方法執(zhí)行完成后立即執(zhí)行下次任務(wù)。
二、配置多個(gè)定時(shí)任務(wù)并發(fā)執(zhí)行
1、并行or串行?
缺省狀態(tài)下,當(dāng)我們沒(méi)有給定時(shí)任務(wù)配置線程池時(shí),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
可以看出來(lái)只有一個(gè)線程穿行執(zhí)行所有定時(shí)任務(wù)。
2、Schedule并行執(zhí)行配置
定時(shí)調(diào)度的并行化,有兩種配置方式:
1)修改任務(wù)調(diào)度器默認(rèn)使用的線程池:添加一個(gè)configuration,實(shí)現(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ù)交給一步線程池處理:?jiǎn)⒂聾EnableAsync注解,并在每一個(gè)定時(shí)任務(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
有上面輸出可以看出來(lái)這種方式對(duì)于每一次定時(shí)任務(wù)的執(zhí)行都會(huì)創(chuàng)建新的線程,這樣對(duì)內(nèi)存資源是一種浪費(fèi),嚴(yán)重情況下還會(huì)導(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)容,是不是對(duì)SpringBoot如何配置和使用Schedule有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前文章:SpringBoot如何配置和使用Schedule
分享地址:http://aaarwkj.com/article14/ipdige.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、建站公司、自適應(yīng)網(wǎng)站、云服務(wù)器、小程序開(kāi)發(fā)、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)