Spring Boot定時(shí)任務(wù)單線程多線程實(shí)現(xiàn)代碼解析
1、創(chuàng)建定時(shí)任務(wù):
@Componentpublic class AutoNotifyController { /** * 獲取RedisUtils注入的bean * * @return */ private ThreadUtil getThreadUtil() { ThreadUtil threadUtil = SpringContextUtil.getBean('threadUtil'); return threadUtil; } /** * @描述: 推送啟動(dòng)充電結(jié)果的自動(dòng)獲取和處理分發(fā)方法 * @輸入值: void * @返回值: void */ @Scheduled(cron = '*/5 * * * * ?') public void AutoNotifyStartChargeResult() { getThreadUtil().AutoNotifyStartChargeResult(); } /** * @描述: 推送充電狀態(tài)的自動(dòng)獲取和處理分發(fā)方法 * @輸入值: void * @返回值: void */ @Scheduled(cron = '*/50 * * * * ?') public void AutoNotifyChargeStatus() { getThreadUtil().AutoNotifyChargeStatus(); } /** * @描述: 推送停止充電結(jié)果的自動(dòng)獲取和處理分發(fā)方法 * @輸入值: void * @返回值: void */ @Scheduled(cron = '*/5 * * * * ?') public void AutoNotifyStopChargeResult() { getThreadUtil().AutoNotifyStopChargeResult(); } /** * @描述: 推送訂單信息的自動(dòng)獲取和處理分發(fā)方法 * @輸入值: void * @返回值: void */ @Scheduled(cron = '*/5 * * * * ?') public void AutoNotifyOrderInfo() { getThreadUtil().AutoNotifyOrderInfo(); } /** * @描述: 公共信息部分的設(shè)備狀態(tài)變化推送接口的自動(dòng)獲取和處理分發(fā)方法 * @輸入值: void * @返回值: void */ @Scheduled(fixedRate = 200) public void checkGunStatus() { getThreadUtil().checkGunStatus(); } /** * @描述: 對(duì)于Redis中的活躍訂單增加和刪除的輪詢執(zhí)行方法 */ @Scheduled(cron = '*/5 * * * * ?') public void ActiveOrderAddAndDelete() { getThreadUtil().ActiveOrderAddAndDelete(); } /** * @描述: 對(duì)于Redis中的結(jié)束訂單訂單增加和刪除的輪詢執(zhí)行方法 */ @Scheduled(cron = '*/5 * * * * ?') public void EndOrderAddAndDelete() { getThreadUtil().EndOrderAddAndDelete(); }}
使用 @Scheduled來創(chuàng)建定時(shí)任務(wù) 這個(gè)注解用來標(biāo)注一個(gè)定時(shí)任務(wù)方法。
通過看 @Scheduled源碼可以看出它支持多種參數(shù):
(1)cron:cron表達(dá)式,指定任務(wù)在特定時(shí)間執(zhí)行;
(2)fixedDelay:表示上一次任務(wù)執(zhí)行完成后多久再次執(zhí)行,參數(shù)類型為long,單位ms;
(3)fixedDelayString:與fixedDelay含義一樣,只是參數(shù)類型變?yōu)镾tring;
(4)fixedRate:表示按一定的頻率執(zhí)行任務(wù),參數(shù)類型為long,單位ms;
(5)fixedRateString: 與fixedRate的含義一樣,只是將參數(shù)類型變?yōu)镾tring;
(6)initialDelay:表示延遲多久再第一次執(zhí)行任務(wù),參數(shù)類型為long,單位ms;
(7)initialDelayString:與initialDelay的含義一樣,只是將參數(shù)類型變?yōu)镾tring;
(8)zone:時(shí)區(qū),默認(rèn)為當(dāng)前時(shí)區(qū),一般沒有用到。
2、開啟定時(shí)任務(wù):
@SpringBootApplication@EnableSchedulingpublic class PositivebuttjointApplication extends SpringBootServletInitializer{ public static void main(String[] args) { SpringApplication.run(PositivebuttjointApplication.class, args); }
注:這里的 @EnableScheduling 注解,它的作用是發(fā)現(xiàn)注解 @Scheduled的任務(wù)并由后臺(tái)執(zhí)行。沒有它的話將無法執(zhí)行定時(shí)任務(wù)。
引用官方文檔原文:
@EnableScheduling ensures that a background task executor is created. Without it, nothing gets scheduled.
3、執(zhí)行結(jié)果(單線程)
就完成了一個(gè)簡(jiǎn)單的定時(shí)任務(wù)模型,下面執(zhí)行springBoot觀察執(zhí)行結(jié)果:
從控制臺(tái)輸入的結(jié)果中我們可以看出所有的定時(shí)任務(wù)都是在同一個(gè)線程池用同一個(gè)線程來處理的,那么我們?nèi)绾蝸聿l(fā)的處理各定時(shí)任務(wù)呢,請(qǐng)繼續(xù)向下看。
4、多線程處理定時(shí)任務(wù):
1.開啟多線程
@SpringBootApplication@EnableScheduling@EnableAsyncpublic class PositivebuttjointApplication extends SpringBootServletInitializer{ public static void main(String[] args) { SpringApplication.run(PositivebuttjointApplication.class, args); }
加入@EnableAsync開啟多線程
2.使用多線程
@Async public void AutoNotifyStartChargeResult() { }
調(diào)用的方法上加上@Async使用多線程
3.配置連接池
@Configurationpublic class ScheduleConfiguration implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.setScheduler(this.getTaskScheduler()); } private ThreadPoolTaskScheduler getTaskScheduler() { ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); taskScheduler.setPoolSize(20); taskScheduler.setThreadNamePrefix('schedule-pool-'); taskScheduler.initialize(); return taskScheduler; }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼2. XML入門的常見問題(一)3. ASP實(shí)現(xiàn)加法驗(yàn)證碼4. ASP中if語句、select 、while循環(huán)的使用方法5. ASP.NET MVC使用異步Action的方法6. 匹配模式 - XSL教程 - 47. ASP.NET MVC通過勾選checkbox更改select的內(nèi)容8. JS中map和parseInt的用法詳解9. XML入門精解之結(jié)構(gòu)與語法10. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera
