javascript
SpringBoot中定时任务与异步定时任务的实现
場景
若依前后端分離版手把手教你本地搭建環境并運行項目:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662
在上面實現項目搭建的基礎上,怎樣在SpringBoot中實現定時任務與異步定時任務實現。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
定時任務實現
新建一個類,類上添加
@Component @EnableScheduling注解開啟定時任務支持。
然后類中新建方法,使用
@Scheduled(fixedRateString = "1000")來標識定時任務執行的方法。
這里使用fixedRateString是因為這樣可以通過
@Scheduled(fixedRateString = "${scheduled.WebRedisPT}")這種類似的方式可以從配置文件中讀取配置的定時任務執行的頻率。
然后不使用cron表達式方式執行定時任務方法,是因為這樣可以設置定時任務的方法
在一秒內執行多次,這里是1000代表一秒執行1次,如果是500則代表一秒執行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("任務1執行開始時間"+ dateFormat.format(System.currentTimeMillis()));System.out.println("公眾號:霸道的程序猿");System.out.println("任務1執行結束時間"+ dateFormat.format(System.currentTimeMillis()));}}執行效果
?
異步定時任務執行
當一個系統中需要執行的定時任務比較多且每個任務執行的頻率比較快時,如果還是用上面那種方式去實現所有定時任務的話,就會出現線程擁堵、定時任務在指定的時間內執行不完的情況,這時候就得需要異步任務的執行。
首先新建一個配置類來開啟異步定時任務的支持
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();//配置核心線程數executor.setCorePoolSize(32);//配置最大線程數executor.setMaxPoolSize(60);//配置隊列大小executor.setQueueCapacity(99999);//配置線程池中的線程的名稱前綴executor.setThreadNamePrefix("async-service-");// rejection-policy:當pool已經達到max size的時候,如何處理新任務// CALLER_RUNS:不在新線程中執行任務,而是有調用者所在的線程來執行executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());//執行初始化executor.initialize();return executor;} }這里的核心線程數和最大線程數根據自己業務需要和服務器配置自行修改。
然后再新建一個定時任務執行類,除了添加
@Component @EnableScheduling在類上以及添加
@Scheduled(fixedRateString = "500")還要在方法上添加
@Async("asyncServiceExecutor")開始異步定時任務的支持
完整示例代碼
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("異步定時任務執行");} }?
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的SpringBoot中定时任务与异步定时任务的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows中获取Redis指定前缀的
- 下一篇: CentOS中升级openssl与卸载重