javascript
SpringBoot中定时任务与异步定时任务的实现
場(chǎng)景
若依前后端分離版手把手教你本地搭建環(huán)境并運(yùn)行項(xiàng)目:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662
在上面實(shí)現(xiàn)項(xiàng)目搭建的基礎(chǔ)上,怎樣在SpringBoot中實(shí)現(xiàn)定時(shí)任務(wù)與異步定時(shí)任務(wù)實(shí)現(xiàn)。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號(hào)
霸道的程序猿
獲取編程相關(guān)電子書(shū)、教程推送與免費(fèi)下載。
實(shí)現(xiàn)
定時(shí)任務(wù)實(shí)現(xiàn)
新建一個(gè)類(lèi),類(lèi)上添加
@Component @EnableScheduling注解開(kāi)啟定時(shí)任務(wù)支持。
然后類(lèi)中新建方法,使用
@Scheduled(fixedRateString = "1000")來(lái)標(biāo)識(shí)定時(shí)任務(wù)執(zhí)行的方法。
這里使用fixedRateString是因?yàn)檫@樣可以通過(guò)
@Scheduled(fixedRateString = "${scheduled.WebRedisPT}")這種類(lèi)似的方式可以從配置文件中讀取配置的定時(shí)任務(wù)執(zhí)行的頻率。
然后不使用cron表達(dá)式方式執(zhí)行定時(shí)任務(wù)方法,是因?yàn)檫@樣可以設(shè)置定時(shí)任務(wù)的方法
在一秒內(nèi)執(zhí)行多次,這里是1000代表一秒執(zhí)行1次,如果是500則代表一秒執(zhí)行2次。
示例代碼
package com.ruoyi.web.asyncTask;import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;@Component @EnableScheduling public class TaskDemo {private SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSS");@Scheduled(fixedRateString = "1000")public void taskOne() {System.out.println("任務(wù)1執(zhí)行開(kāi)始時(shí)間"+ dateFormat.format(System.currentTimeMillis()));System.out.println("公眾號(hào):霸道的程序猿");System.out.println("任務(wù)1執(zhí)行結(jié)束時(shí)間"+ dateFormat.format(System.currentTimeMillis()));}}執(zhí)行效果
?
異步定時(shí)任務(wù)執(zhí)行
當(dāng)一個(gè)系統(tǒng)中需要執(zhí)行的定時(shí)任務(wù)比較多且每個(gè)任務(wù)執(zhí)行的頻率比較快時(shí),如果還是用上面那種方式去實(shí)現(xiàn)所有定時(shí)任務(wù)的話(huà),就會(huì)出現(xiàn)線程擁堵、定時(shí)任務(wù)在指定的時(shí)間內(nèi)執(zhí)行不完的情況,這時(shí)候就得需要異步任務(wù)的執(zhí)行。
首先新建一個(gè)配置類(lèi)來(lái)開(kāi)啟異步定時(shí)任務(wù)的支持
package com.ruoyi.web.config;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor;@Configuration @EnableAsync public class ExecutorConfig {private static final Logger logger = LoggerFactory.getLogger(ExecutorConfig.class);@Beanpublic Executor asyncServiceExecutor() {logger.info("start asyncServiceExecutor");ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();//配置核心線程數(shù)executor.setCorePoolSize(32);//配置最大線程數(shù)executor.setMaxPoolSize(60);//配置隊(duì)列大小executor.setQueueCapacity(99999);//配置線程池中的線程的名稱(chēng)前綴executor.setThreadNamePrefix("async-service-");// rejection-policy:當(dāng)pool已經(jīng)達(dá)到max size的時(shí)候,如何處理新任務(wù)// CALLER_RUNS:不在新線程中執(zhí)行任務(wù),而是有調(diào)用者所在的線程來(lái)執(zhí)行executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//執(zhí)行初始化executor.initialize();return executor;} }這里的核心線程數(shù)和最大線程數(shù)根據(jù)自己業(yè)務(wù)需要和服務(wù)器配置自行修改。
然后再新建一個(gè)定時(shí)任務(wù)執(zhí)行類(lèi),除了添加
@Component @EnableScheduling在類(lèi)上以及添加
@Scheduled(fixedRateString = "500")還要在方法上添加
@Async("asyncServiceExecutor")開(kāi)始異步定時(shí)任務(wù)的支持
完整示例代碼
package com.ruoyi.web.asyncTask;import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;@Component @EnableScheduling public class asyncTaskDemo {@Scheduled(fixedRateString = "500")@Async("asyncServiceExecutor")public void taskOne() {System.out.println("異步定時(shí)任務(wù)執(zhí)行");} }?
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的SpringBoot中定时任务与异步定时任务的实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Windows中获取Redis指定前缀的
- 下一篇: CentOS中升级openssl与卸载重