Spring Boot定時(shí)任務(wù)單線程多線程實(shí)現(xiàn)代碼解析
1、創(chuàng)建定時(shí)任務(wù):
@Component
public 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中的活躍訂單增加和刪除的輪詢(xún)執(zhí)行方法
*/
@Scheduled(cron = "*/5 * * * * ?")
public void ActiveOrderAddAndDelete() {
getThreadUtil().ActiveOrderAddAndDelete();
}
/**
* @描述: 對(duì)于Redis中的結(jié)束訂單訂單增加和刪除的輪詢(xún)執(zhí)行方法
*/
@Scheduled(cron = "*/5 * * * * ?")
public void EndOrderAddAndDelete() {
getThreadUtil().EndOrderAddAndDelete();
}
}
使用 @Scheduled來(lái)創(chuàng)建定時(shí)任務(wù) 這個(gè)注解用來(lái)標(biāo)注一個(gè)定時(shí)任務(wù)方法。
通過(guò)看 @Scheduled源碼可以看出它支持多種參數(shù):
(1)cron:cron表達(dá)式,指定任務(wù)在特定時(shí)間執(zhí)行;
(2)fixedDelay:表示上一次任務(wù)執(zhí)行完成后多久再次執(zhí)行,參數(shù)類(lèi)型為long,單位ms;
(3)fixedDelayString:與fixedDelay含義一樣,只是參數(shù)類(lèi)型變?yōu)镾tring;
(4)fixedRate:表示按一定的頻率執(zhí)行任務(wù),參數(shù)類(lèi)型為long,單位ms;
(5)fixedRateString: 與fixedRate的含義一樣,只是將參數(shù)類(lèi)型變?yōu)镾tring;
(6)initialDelay:表示延遲多久再第一次執(zhí)行任務(wù),參數(shù)類(lèi)型為long,單位ms;
(7)initialDelayString:與initialDelay的含義一樣,只是將參數(shù)類(lèi)型變?yōu)镾tring;
(8)zone:時(shí)區(qū),默認(rèn)為當(dāng)前時(shí)區(qū),一般沒(méi)有用到。
2、開(kāi)啟定時(shí)任務(wù):
@SpringBootApplication
@EnableScheduling
public class PositivebuttjointApplication extends SpringBootServletInitializer
{
public static void main(String[] args)
{
SpringApplication.run(PositivebuttjointApplication.class, args);
}
注:這里的 @EnableScheduling 注解,它的作用是發(fā)現(xiàn)注解 @Scheduled的任務(wù)并由后臺(tái)執(zhí)行。沒(méi)有它的話(huà)將無(wú)法執(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è)線程來(lái)處理的,那么我們?nèi)绾蝸?lái)并發(fā)的處理各定時(shí)任務(wù)呢,請(qǐng)繼續(xù)向下看。
4、多線程處理定時(shí)任務(wù):
1.開(kāi)啟多線程
@SpringBootApplication
@EnableScheduling
@EnableAsync
public class PositivebuttjointApplication extends SpringBootServletInitializer
{
public static void main(String[] args)
{
SpringApplication.run(PositivebuttjointApplication.class, args);
}
加入@EnableAsync開(kāi)啟多線程
2.使用多線程
@Async
public void AutoNotifyStartChargeResult() {
}
調(diào)用的方法上加上@Async使用多線程
3.配置連接池
@Configuration
public 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í)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot定時(shí)任務(wù)參數(shù)運(yùn)行代碼實(shí)例解析
- SpringBoot整合SpringTask實(shí)現(xiàn)定時(shí)任務(wù)的流程
- Spring Boot @Scheduled定時(shí)任務(wù)代碼實(shí)例解析
- Spring boot基于ScheduledFuture實(shí)現(xiàn)定時(shí)任務(wù)
- SpringBoot集成Quartz實(shí)現(xiàn)定時(shí)任務(wù)的方法
- Spring Boot監(jiān)聽(tīng)Redis Key失效事件實(shí)現(xiàn)定時(shí)任務(wù)的示例
- springboot實(shí)現(xiàn)多實(shí)例crontab搶占定時(shí)任務(wù)(實(shí)例代碼)
- SpringBoot基于數(shù)據(jù)庫(kù)的定時(shí)任務(wù)統(tǒng)一管理的實(shí)現(xiàn)
相關(guān)文章
java gui實(shí)現(xiàn)計(jì)算器小程序
這篇文章主要為大家詳細(xì)介紹了java gui實(shí)現(xiàn)計(jì)算器小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Java 基于Jakarta Mail實(shí)現(xiàn)收發(fā)郵件
這篇文章主要介紹了Java 基于Jakarta Mail實(shí)現(xiàn)收發(fā)郵件的功能,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-04-04
Java多線程案例實(shí)戰(zhàn)之定時(shí)器的實(shí)現(xiàn)
在Java中可以使用多線程和定時(shí)器來(lái)實(shí)現(xiàn)定時(shí)任務(wù),下面這篇文章主要給大家介紹了關(guān)于Java多線程案例之定時(shí)器實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
Spring Boot應(yīng)用上傳文件時(shí)報(bào)錯(cuò)的原因及解決方案
這篇文章主要介紹了Spring Boot應(yīng)用上傳文件時(shí)報(bào)錯(cuò)的原因及解決方案,幫助大家更好的理解和學(xué)習(xí)使用spring boot框架,感興趣的朋友可以了解下2021-02-02
Java實(shí)現(xiàn)自動(dòng)壓縮文件并加密的方法示例
這篇文章主要介紹了Java實(shí)現(xiàn)自動(dòng)壓縮文件并加密的方法,涉及java針對(duì)文件進(jìn)行zip壓縮并加密的相關(guān)操作技巧,需要的朋友可以參考下2018-01-01

