SpringBoot實(shí)現(xiàn)定時(shí)任務(wù)的三種方式小結(jié)
SpringBoot三種方式實(shí)現(xiàn)定時(shí)任務(wù)
定時(shí)任務(wù)實(shí)現(xiàn)的三種方式
Timer
:這是java自帶的java.util.Timer類,這個(gè)類允許你調(diào)度一個(gè)java.util.TimerTask任務(wù)。使用這種方式可以讓你的程序按照某一個(gè)頻度執(zhí)行,但不能在指定時(shí)間運(yùn)行。一般用的較少。ScheduledExecutorService
:也jdk自帶的一個(gè)類;是基于線程池設(shè)計(jì)的定時(shí)任務(wù)類,每個(gè)調(diào)度任務(wù)都會(huì)分配到線程池中的一個(gè)線程去執(zhí)行,也就是說(shuō),任務(wù)是并發(fā)執(zhí)行,互不影響。Spring Task
:Spring3.0以后自帶的task,可以將它看成一個(gè)輕量級(jí)的Quartz,而且使用起來(lái)比Quartz簡(jiǎn)單許多。
使用Timer
這個(gè)目前在項(xiàng)目中用的較少,直接貼demo代碼。具體的介紹可以查看api
public class TestTimer { public static void main(String[] args) { TimerTask timerTask = new TimerTask() { @Override public void run() { System.out.println("task run:"+ new Date()); } }; Timer timer = new Timer(); //安排指定的任務(wù)在指定的時(shí)間開(kāi)始進(jìn)行重復(fù)的固定延遲執(zhí)行。這里是每3秒執(zhí)行一次 timer.schedule(timerTask,10,3000); } }
使用ScheduledExecutorService
該方法跟Timer類似,直接看demo:
public class TestScheduledExecutorService { public static void main(String[] args) { ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); // 參數(shù):1、任務(wù)體 2、首次執(zhí)行的延時(shí)時(shí)間 // 3、任務(wù)執(zhí)行間隔 4、間隔時(shí)間單位 service.scheduleAtFixedRate(()->System.out.println("task ScheduledExecutorService "+new Date()), 0, 3, TimeUnit.SECONDS); } }
使用Spring Task
1.簡(jiǎn)單的定時(shí)任務(wù)
在SpringBoot項(xiàng)目中,我們可以很優(yōu)雅的使用注解來(lái)實(shí)現(xiàn)定時(shí)任務(wù),首先創(chuàng)建項(xiàng)目,導(dǎo)入依賴:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
創(chuàng)建任務(wù)類:
@Slf4j @Component public class ScheduledService { @Scheduled(cron = "0/5 * * * * *") public void scheduled(){ log.info("=====>>>>>使用cron {}",System.currentTimeMillis()); } @Scheduled(fixedRate = 5000) public void scheduled1() { log.info("=====>>>>>使用fixedRate{}", System.currentTimeMillis()); } @Scheduled(fixedDelay = 5000) public void scheduled2() { log.info("=====>>>>>fixedDelay{}",System.currentTimeMillis()); } }
在主類上使用@EnableScheduling注解開(kāi)啟對(duì)定時(shí)任務(wù)的支持,然后啟動(dòng)項(xiàng)目
可以看到三個(gè)定時(shí)任務(wù)都已經(jīng)執(zhí)行,并且使同一個(gè)線程中串行執(zhí)行,如果只有一個(gè)定時(shí)任務(wù),這樣做肯定沒(méi)問(wèn)題,當(dāng)定時(shí)任務(wù)增多,如果一個(gè)任務(wù)卡死,會(huì)導(dǎo)致其他任務(wù)也無(wú)法執(zhí)行。
2.多線程執(zhí)行
在傳統(tǒng)的Spring項(xiàng)目中,我們可以在xml配置文件添加task的配置,而在SpringBoot項(xiàng)目中一般使用config配置類的方式添加配置,所以新建一個(gè)AsyncConfig類
@Configuration @EnableAsync public class AsyncConfig { /* 此處成員變量應(yīng)該使用@Value從配置中讀取 */ private int corePoolSize = 10; private int maxPoolSize = 200; private int queueCapacity = 10; @Bean public Executor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(corePoolSize); executor.setMaxPoolSize(maxPoolSize); executor.setQueueCapacity(queueCapacity); executor.initialize(); return executor; } }
@Configuration
:表明該類是一個(gè)配置類@EnableAsync
:開(kāi)啟異步事件的支持
然后在定時(shí)任務(wù)的類或者方法上添加@Async 。最后重啟項(xiàng)目,每一個(gè)任務(wù)都是在不同的線程中。
在線cron表達(dá)式生成:http://qqe2.com/cron/index
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot定時(shí)任務(wù)兩種(Spring Schedule 與 Quartz 整合 )實(shí)現(xiàn)方法
- SpringBoot中實(shí)現(xiàn)定時(shí)任務(wù)的4種方式詳解
- SpringBoot中實(shí)現(xiàn)定時(shí)任務(wù)的幾種方式
- SpringBoot2 task scheduler 定時(shí)任務(wù)調(diào)度器四種方式
- SpringBoot下使用定時(shí)任務(wù)的方式全揭秘(6種)
- SpringBoot實(shí)現(xiàn)固定和動(dòng)態(tài)定時(shí)任務(wù)的三種方法
- Springboot實(shí)現(xiàn)定時(shí)任務(wù)的4種方式舉例詳解
- SpringBoot最新定時(shí)任務(wù)的7種實(shí)現(xiàn)方案
相關(guān)文章
Spring?Data?JPA命名約定查詢實(shí)現(xiàn)方法
這篇文章主要為大家介紹了Spring?Data?JPA命名約定查詢實(shí)現(xiàn)方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Spring詳細(xì)講解事務(wù)失效的場(chǎng)景
實(shí)際項(xiàng)目開(kāi)發(fā)中,如果涉及到多張表操作時(shí),為了保證業(yè)務(wù)數(shù)據(jù)的一致性,大家一般都會(huì)采用事務(wù)機(jī)制,好多小伙伴可能只是簡(jiǎn)單了解一下,遇到事務(wù)失效的情況,便會(huì)無(wú)從下手,下面這篇文章主要給大家介紹了關(guān)于Spring事務(wù)失效場(chǎng)景的相關(guān)資料,需要的朋友可以參考下2022-07-07關(guān)于JSON解析中獲取不存在的key問(wèn)題
這篇文章主要介紹了關(guān)于JSON解析中獲取不存在的key問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Spring Boot管理用戶數(shù)據(jù)的操作步驟
SpringBoot結(jié)合Thymeleaf模板引擎,可以快速搭建Web應(yīng)用,介紹了使用SpringBoot處理JSON數(shù)據(jù)的基本過(guò)程,包括創(chuàng)建實(shí)體類、視圖頁(yè)面和控制器,通過(guò)這些步驟,即可完成基于SpringBoot和Thymeleaf的簡(jiǎn)單Web開(kāi)發(fā),感興趣的朋友跟隨小編一起看看吧2024-09-09使用Netty解決TCP粘包和拆包問(wèn)題過(guò)程詳解
這篇文章主要介紹了使用Netty解決TCP粘包和拆包問(wèn)題過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07Java通過(guò)SMS短信平臺(tái)實(shí)現(xiàn)發(fā)短信功能 含多語(yǔ)言
這篇文章主要為大家詳細(xì)介紹了Java通過(guò)SMS短信平臺(tái)實(shí)現(xiàn)發(fā)短信功能的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07Spring?Boot整合持久層之JPA多數(shù)據(jù)源
JPA(Java Persistence API)Java 持久化 API,是 Java 持久化的標(biāo)準(zhǔn)規(guī)范,Hibernate 是持久化規(guī)范的技術(shù)實(shí)現(xiàn),而 Spring Data JPA 是在 Hibernate 基礎(chǔ)上封裝的一款框架2022-08-08