SpringBoot配置及使用Schedule過(guò)程解析
我們?cè)谄匠m?xiàng)目開(kāi)發(fā)中,經(jīng)常會(huì)用到周期性定時(shí)任務(wù),這個(gè)時(shí)候使用定時(shí)任務(wù)就能很方便的實(shí)現(xiàn)。在SpringBoot中用得最多的就是Schedule。
一、SpringBoot集成Schedule
1、依賴配置
由于Schedule就包含在spring-boot-starter中,所以無(wú)需引入其他依賴。
2、啟用定時(shí)任務(wù)
在啟動(dòng)類或者配置類上增加@EnableScheduling注解。
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableScheduling;@EnableScheduling@SpringBootApplicationpublic 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@EnableSchedulingpublic 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 execute2020-04-23 23:11:58.365 INFO 85325 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed delay schedule execute2020-04-23 23:12:02.372 INFO 85325 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed delay schedule execute2020-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 execute2020-04-23 23:16:17.754 INFO 85328 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed rate schedule execute2020-04-23 23:16:20.760 INFO 85328 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed rate schedule execute2020-04-23 23:16:23.760 INFO 85328 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : fixed rate schedule execute2020-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@EnableSchedulingpublic 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 execute2020-04-23 23:19:48.973 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task2 execute2020-04-23 23:19:50.974 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task3 execute2020-04-23 23:19:52.978 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task1 execute2020-04-23 23:19:54.984 INFO 85332 --- [ scheduling-1] com.springboot.study.tasks.MyCronTask : task2 execute2020-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接口就可以了。
@Configurationpublic 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 execute2020-04-23 23:33:14.197 INFO 85461 --- [ myworker-1] com.springboot.study.tasks.MyCronTask : task1 execute2020-04-23 23:33:14.197 INFO 85461 --- [ myworker-3] com.springboot.study.tasks.MyCronTask : task3 execute2020-04-23 23:33:18.203 INFO 85461 --- [ myworker-2] com.springboot.study.tasks.MyCronTask : task2 execute2020-04-23 23:33:18.203 INFO 85461 --- [ myworker-3] com.springboot.study.tasks.MyCronTask : task1 execute2020-04-23 23:33:18.204 INFO 85461 --- [ myworker-1] com.springboot.study.tasks.MyCronTask : task3 execute2020-04-23 23:33:22.208 INFO 85461 --- [ myworker-1] com.springboot.study.tasks.MyCronTask : task3 execute2020-04-23 23:33:22.208 INFO 85461 --- [ myworker-2] com.springboot.study.tasks.MyCronTask : task2 execute2020-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@Asyncpublic 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 execute2020-04-23 23:38:00.614 INFO 85468 --- [ task-3] com.springboot.study.tasks.MyCronTask : task3 execute2020-04-23 23:38:00.614 INFO 85468 --- [ task-2] com.springboot.study.tasks.MyCronTask : task2 execute2020-04-23 23:38:02.620 INFO 85468 --- [ task-4] com.springboot.study.tasks.MyCronTask : task1 execute2020-04-23 23:38:02.620 INFO 85468 --- [ task-5] com.springboot.study.tasks.MyCronTask : task2 execute2020-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ù)掛掉,因此為了更好控制線程的使用,我們可以自定義線程池。
首先配置線程池:
@Configurationpublic 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 execute2020-04-23 23:46:47.404 INFO 85488 --- [ myExecutor-3] com.springboot.study.tasks.MyCronTask : task3 execute2020-04-23 23:46:47.404 INFO 85488 --- [ myExecutor-2] com.springboot.study.tasks.MyCronTask : task2 execute2020-04-23 23:46:49.404 INFO 85488 --- [ myExecutor-3] com.springboot.study.tasks.MyCronTask : task2 execute2020-04-23 23:46:49.404 INFO 85488 --- [ myExecutor-2] com.springboot.study.tasks.MyCronTask : task3 execute2020-04-23 23:46:49.404 INFO 85488 --- [ myExecutor-1] com.springboot.study.tasks.MyCronTask : task1 execute2020-04-23 23:46:51.405 INFO 85488 --- [ myExecutor-2] com.springboot.study.tasks.MyCronTask : task2 execute2020-04-23 23:46:51.405 INFO 85488 --- [ myExecutor-3] com.springboot.study.tasks.MyCronTask : task1 execute2020-04-23 23:46:51.405 INFO 85488 --- [ myExecutor-1] com.springboot.study.tasks.MyCronTask : task3 execute
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. idea設(shè)置自動(dòng)導(dǎo)入依賴的方法步驟2. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐3. XML入門的常見(jiàn)問(wèn)題(一)4. jsp EL表達(dá)式詳解5. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實(shí)現(xiàn)6. Django ORM實(shí)現(xiàn)按天獲取數(shù)據(jù)去重求和例子7. idea給項(xiàng)目打war包的方法步驟8. chat.asp聊天程序的編寫方法9. idea修改背景顏色樣式的方法10. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?
