SpringBoot項(xiàng)目使用內(nèi)置的單機(jī)任務(wù)調(diào)度功能詳解
SpringBoot項(xiàng)目使用內(nèi)置的單機(jī)任務(wù)調(diào)度功能
SpringBoot框架中提供了2個注解來讓開發(fā)者快速配置來實(shí)現(xiàn)單機(jī)定時任務(wù)調(diào)度的功能。
分別是@EnableScheduling和 @Scheduled
配置EnableScheduling注解
@EnableScheduling注解意思是啟用SpringBoot框架的單機(jī)定時任務(wù)調(diào)度的功能,這個注解要配置到app的啟動類上,或者你自定義的@Configuration配置類上,一般都配置到項(xiàng)目的啟動類上,方便查找
如下代碼所示
package cn.thinkpet.springbootappscaffold; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling //在啟動類上加這個注解,表示開啟springboot定時任務(wù)功能 @SpringBootApplication public class SpringbootAppScaffoldApplication { public static void main(String[] args) { SpringApplication.run(SpringbootAppScaffoldApplication.class, args); } }
定義1個組件bean,用來存放要執(zhí)行調(diào)度的task任務(wù)
package cn.thinkpet.springbootappscaffold; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Slf4j @Component public class Test1Schedule { //@Scheduled注解是springboot提供的單機(jī)應(yīng)用的定時任務(wù)調(diào)度解決方案 //cron是配置定時任務(wù)調(diào)度的 spring-cron表達(dá)式 // spring-cron 一共可以有6個參數(shù) 以空格分開 // 注意spring-cron 和quartz的cron不同 ,即 spring-cron不支持配置年份,沒有第7個參數(shù), // 如果需要指定年份的定時任務(wù)建議使用quartz,xxl-job,elastic-job這種分布式任務(wù)調(diào)度框架 // 秒 分 時 日 月 周 //┌───────────── second (0-59) // │ ┌───────────── minute (0 - 59) // │ │ ┌───────────── hour (0 - 23) // │ │ │ ┌───────────── day of the month (1 - 31) // │ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC) // │ │ │ │ │ ┌───────────── day of the week (0 - 7) // │ │ │ │ │ │ (0 or 7 is Sunday, or MON-SUN) // │ │ │ │ │ │ // * * * * * * // @Scheduled(cron = "0 41 21 * * ?") // 定點(diǎn)--21點(diǎn)41分0秒 執(zhí)行 // @Scheduled(cron = "*/1 * * * * ?") //定頻--每1秒執(zhí)行1次 // 0 0 0 ? * MON#1 每月第一個星期一的零點(diǎn)0分0秒 // 0 0 0 ? * 5#2 每月第2個星期五的零點(diǎn)0分0秒 // 0 0 0 L * * 每月最后一天的零點(diǎn)0分0秒 // 0 0 0 * * 5L 每月最后一個星期五的零點(diǎn)0分0秒 //spring-cron表達(dá)式 值的常用通配符: // *:表示所有值 比如用在日 表示每一天。 // ?:表示不指定值 比如周配置 表示不指定星期幾執(zhí)行。 // /:表示遞增觸發(fā) 比如 用在分 5/20 從第五分鐘開始 每增加20分鐘執(zhí)行一次。 // -:表示區(qū)間 比如用在 1-6 表示一月到六月執(zhí)行 // # :表示每月中的第幾個星期幾。5#2:表示每月第2個星期五。MON#1:表示每月第1個星期一 // L :表示最后,比如每月最后一個星期天 //其他通配符可以查詢官方文檔 //https://docs.spring.io/spring-framework/reference/integration/scheduling.html#scheduling-cron-expression @Scheduled(cron = "0/1 * * * * ?") public void singleTaskTest1() throws InterruptedException { log.info("singleTaskTest1"); } @Scheduled(cron = "0/1 * * * * ?") public void singleTaskTest2() throws InterruptedException { log.info("singleTaskTest2"); } // @Scheduled(cron = "0/1 * * * * ?") @Scheduled(cron = "${test3.cron:0/2 * * * * ?}") //從yml或properties配置文件中讀取 cron 值,讀不到時使用默認(rèn)的 0/2 * * * * ? public void singleTaskTest3() throws InterruptedException { log.info("singleTaskTest3"); // LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(3)); } //----------如果嫌配置cron表達(dá)式麻煩,可以使用如下的固定速度+固定延期 的方式來配置定時任務(wù)----------- // @Scheduled(initialDelay = 3000,fixedRate = 5000) //initialDelay表示第一次延遲多少毫秒執(zhí)行,單位是毫秒 //fixedRate表示多久執(zhí)行一次,單位是毫秒 @Scheduled(initialDelay = 3000,fixedRate = 5000) //第一次延遲3秒執(zhí)行,后面間隔5秒執(zhí)行一次 public void singleTaskTest4() throws InterruptedException { log.info("singleTaskTest4"); } }
application.properties文件的配置
server.port=8080 #配置singleTaskTest3的cron表達(dá)式 test3.cron=0/7 * * * * ?
重寫SpringBoot任務(wù)調(diào)度使用的線程池執(zhí)行器
在實(shí)際開發(fā)中,常常會有多個task并發(fā)執(zhí)行的場景,此時為了實(shí)現(xiàn)task之間的異步,需要自定義調(diào)度器使用的 ThreadPoolTaskScheduler
package cn.thinkpet.springbootappscaffold; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.scheduling.config.ScheduledTaskRegistrar; /** * 為了實(shí)現(xiàn)springboot 多個定時任務(wù)調(diào)度 之間的并發(fā)執(zhí)行, * 需要重寫 調(diào)度器使用的 ThreadPoolTaskScheduler */ @Configuration public class MyScheduledConfig implements SchedulingConfigurer { /** * 定義springboot定時任務(wù)的 --- 線程池大小 */ private static final int POOL_SIZE = 6; /** * 定義springboot定時任務(wù)的 ---自定義線程名前綴 */ private static final String TASK_THREAD_PREFIX = "my-sche-task-"; @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { //需要創(chuàng)建1個springboot提供的ThreadPoolTaskScheduler ThreadPoolTaskScheduler bootScheduleExecutor = new ThreadPoolTaskScheduler(); //配置這個springboot項(xiàng)目全部定時任務(wù)使用 的線程數(shù)是 POOL_SIZE //在實(shí)際應(yīng)用中需要考慮實(shí)際任務(wù)數(shù)量,創(chuàng)建相應(yīng)大小的線程池 bootScheduleExecutor.setPoolSize(POOL_SIZE); //配置線程名前綴 bootScheduleExecutor.setThreadNamePrefix(TASK_THREAD_PREFIX); //初始化線程池執(zhí)行器 bootScheduleExecutor.initialize(); //設(shè)置 調(diào)度器使用 bootScheduleExecutor taskRegistrar.setTaskScheduler(bootScheduleExecutor); } }
測試效果
2023-06-04 18:17:03.519 INFO 16872 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-06-04 18:17:03.519 INFO 16872 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService
2023-06-04 18:17:03.534 INFO 16872 --- [ main] c.t.s.SpringbootAppScaffoldApplication : Started SpringbootAppScaffoldApplication in 1.506 seconds (JVM running for 2.224)
2023-06-04 18:17:04.007 INFO 16872 --- [ my-sche-task-1] c.t.springbootappscaffold.Test1Schedule : singleTaskTest2
2023-06-04 18:17:04.007 INFO 16872 --- [ my-sche-task-2] c.t.springbootappscaffold.Test1Schedule : singleTaskTest1
2023-06-04 18:17:05.014 INFO 16872 --- [ my-sche-task-3] c.t.springbootappscaffold.Test1Schedule : singleTaskTest2
2023-06-04 18:17:05.014 INFO 16872 --- [ my-sche-task-4] c.t.springbootappscaffold.Test1Schedule : singleTaskTest1
2023-06-04 18:17:06.006 INFO 16872 --- [ my-sche-task-3] c.t.springbootappscaffold.Test1Schedule : singleTaskTest2
2023-06-04 18:17:06.006 INFO 16872 --- [ my-sche-task-6] c.t.springbootappscaffold.Test1Schedule : singleTaskTest1
2023-06-04 18:17:06.541 INFO 16872 --- [ my-sche-task-1] c.t.springbootappscaffold.Test1Schedule : singleTaskTest4
2023-06-04 18:17:07.013 INFO 16872 --- [ my-sche-task-2] c.t.springbootappscaffold.Test1Schedule : singleTaskTest1
2023-06-04 18:17:07.013 INFO 16872 --- [ my-sche-task-4] c.t.springbootappscaffold.Test1Schedule : singleTaskTest3
2023-06-04 18:17:07.013 INFO 16872 --- [ my-sche-task-5] c.t.springbootappscaffold.Test1Schedule : singleTaskTest2
2023-06-04 18:17:08.002 INFO 16872 --- [ my-sche-task-1] c.t.springbootappscaffold.Test1Schedule : singleTaskTest2
2023-06-04 18:17:08.002 INFO 16872 --- [ my-sche-task-2] c.t.springbootappscaffold.Test1Schedule : singleTaskTest1
2023-06-04 18:17:09.009 INFO 16872 --- [ my-sche-task-6] c.t.springbootappscaffold.Test1Schedule : singleTaskTest1
2023-06-04 18:17:09.009 INFO 16872 --- [ my-sche-task-1] c.t.springbootappscaffold.Test1Schedule : singleTaskTest2
2023-06-04 18:17:10.004 INFO 16872 --- [ my-sche-task-1] c.t.springbootappscaffold.Test1Schedule : singleTaskTest2
2023-06-04 18:17:10.004 INFO 16872 --- [ my-sche-task-5] c.t.springbootappscaffold.Test1Schedule : singleTaskTest1
2023-06-04 18:17:11.011 INFO 16872 --- [ my-sche-task-6] c.t.springbootappscaffold.Test1Schedule : singleTaskTest2
2023-06-04 18:17:11.011 INFO 16872 --- [ my-sche-task-5] c.t.springbootappscaffold.Test1Schedule : singleTaskTest1
2023-06-04 18:17:11.545 INFO 16872 --- [ my-sche-task-2] c.t.springbootappscaffold.Test1Schedule : singleTaskTest4
2023-06-04 18:17:12.002 INFO 16872 --- [ my-sche-task-4] c.t.springbootappscaffold.Test1Schedule : singleTaskTest1
2023-06-04 18:17:12.002 INFO 16872 --- [ my-sche-task-3] c.t.springbootappscaffold.Test1Schedule : singleTaskTest2
2023-06-04 18:17:13.009 INFO 16872 --- [ my-sche-task-1] c.t.springbootappscaffold.Test1Schedule : singleTaskTest1
2023-06-04 18:17:13.009 INFO 16872 --- [ my-sche-task-6] c.t.springbootappscaffold.Test1Schedule : singleTaskTest2
2023-06-04 18:17:14.016 INFO 16872 --- [ my-sche-task-2] c.t.springbootappscaffold.Test1Schedule : singleTaskTest1
2023-06-04 18:17:14.016 INFO 16872 --- [ my-sche-task-4] c.t.springbootappscaffold.Test1Schedule : singleTaskTest3
到此這篇關(guān)于SpringBoot項(xiàng)目使用內(nèi)置的單機(jī)任務(wù)調(diào)度功能詳解的文章就介紹到這了,更多相關(guān)SpringBoot內(nèi)置的單機(jī)任務(wù)調(diào)度功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot多線程與任務(wù)調(diào)度總結(jié)
- SpringBoot整合XxlJob分布式任務(wù)調(diào)度平臺
- SpringBoot定時任務(wù)調(diào)度與爬蟲的配置實(shí)現(xiàn)
- SpringBoot 任務(wù)調(diào)度動態(tài)設(shè)置方式(不用重啟服務(wù))
- Springboot集成任務(wù)調(diào)度實(shí)現(xiàn)過程
- SpringBoot2 task scheduler 定時任務(wù)調(diào)度器四種方式
- SpringBoot任務(wù)調(diào)度器的實(shí)現(xiàn)代碼
相關(guān)文章
LocalDateTime日期時間格式中間多了一個T的問題及解決
這篇文章主要介紹了LocalDateTime日期時間格式中間多了一個T的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03spring?mybatis環(huán)境常量與枚舉轉(zhuǎn)換示例詳解
這篇文章主要為大家介紹了spring?mybatis環(huán)境常量與枚舉轉(zhuǎn)換示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06mybatis中${}和#{}的區(qū)別以及底層原理分析
這篇文章主要介紹了mybatis中${}和#{}的區(qū)別以及底層原理,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05