Spring 定時任務@Scheduled 注解四大參數用法解析
本文主要介紹了在 Spring 框架中使用@Scheduled注解實現定時任務的方法,重點講解了fixedRate、fixedDelay、cron和initialDelay這四個參數的用法,并通過實例代碼進行了詳細說明。
1. fixedRate 參數
參數含義
fixedRate
指定任務固定時間間隔執(zhí)行。如設為 3000 毫秒(3 秒),第一次任務啟動后,后續(xù)任務每隔 3 秒嘗試啟動,不考慮前次任務是否完成。
示例代碼
創(chuàng)建一個簡單的 Spring Boot 項目,并在主類上添加@SpringBootApplication
注解以啟用 Spring Boot 自動配置。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling // 啟用定時任務功能 public class ScheduledDemoApplication { public static void main(String[] args) { SpringApplication.run(ScheduledDemoApplication.class, args); } }
創(chuàng)建一個定時任務類,doTask
方法被@Scheduled
注解修飾,fixedRate
設置為 3000 毫秒。在方法內部,我們首先打印出執(zhí)行次數和當前時間,然后通過Thread.sleep
模擬任務執(zhí)行耗時 5 秒。運行應用程序后,你會發(fā)現盡管任務執(zhí)行時間超過了 3 秒,但每隔 3 秒就會有一次新的任務嘗試啟動,導致任務會重疊執(zhí)行。
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class FixedRateTask { private int count = 0; @Scheduled(fixedRate = 3000) public void doTask() { System.out.println("FixedRateTask - 執(zhí)行次數: " + (++count) + ",當前時間: " + System.currentTimeMillis()); try { // 模擬任務執(zhí)行耗時 5 秒 Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } }
2. fixedDelay 參數
參數含義
與fixedRate
不同,fixedDelay
參數關注的是上一次任務執(zhí)行完成后的延遲時間。也就是說,只有當上一次任務徹底結束后,才會開始計算固定的延遲時間,之后才啟動下一次任務。例如,設置fixedDelay = 3000
毫秒,那么在上一次任務完成后,會等待 3 秒才開始下一次任務。
示例代碼
創(chuàng)建一個FixedDelayTask
類,doTask
方法的fixedDelay
設置為 3000 毫秒,任務模擬執(zhí)行耗時 2 秒。運行后可以看到,每次任務執(zhí)行完成后,會等待 3 秒才開始下一次任務,任務不會重疊執(zhí)行。
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class FixedDelayTask { private int count = 0; @Scheduled(fixedDelay = 3000) public void doTask() { System.out.println("FixedDelayTask - 執(zhí)行次數: " + (++count) + ",當前時間: " + System.currentTimeMillis()); try { // 模擬任務執(zhí)行耗時 2 秒 Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } }
3. cron 參數
參數含義
cron
參數提供了一種高度靈活的定時任務執(zhí)行時間設置方式。它基于 cron 表達式,該表達式由 6 個或 7 個字段組成,分別對應秒、分鐘、小時、日、月、星期(星期可以用數字 0 - 6 表示,其中 0 或 7 代表星期日)以及可選的年。通過合理組合這些字段的值,可以精確地定義任務在何時執(zhí)行。
示例代碼
創(chuàng)建一個CronTask
類,cron
表達式"0 0/5 * * * *"
表示每隔 5 分鐘執(zhí)行一次任務。例如,任務可能在 10:00:00、10:05:00、10:10:00 等時間點執(zhí)行。:
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class CronTask { @Scheduled(cron = "0 0/5 * * * *") public void doTask() { System.out.println("CronTask - 執(zhí)行時間: " + System.currentTimeMillis()); } }
4. initialDelay 參數
參數含義
initialDelay
參數用于指定定時任務在應用啟動后首次執(zhí)行的延遲時間。它可以與fixedRate
、fixedDelay
或cron
等參數結合使用,以便在應用啟動后等待一段時間再開始執(zhí)行定時任務。
示例代碼
創(chuàng)建一個InitialDelayTask
類,、fixedRate
設置為 3000 毫秒,initialDelay
設置為 5000 毫秒。這意味著應用啟動后,會先等待 5 秒,然后才開始按照每 3 秒的固定間隔執(zhí)行任務。
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class InitialDelayTask { private int count = 0; @Scheduled(fixedRate = 3000, initialDelay = 5000) public void doTask() { System.out.println("InitialDelayTask - 執(zhí)行次數: " + (++count) + ",當前時間: " + System.currentTimeMillis()); } }
5. 總結
- 對于一些對實時性要求較高且執(zhí)行時間較短的任務,可以考慮使用
fixedRate
; - 對于執(zhí)行時間較長且不希望任務重疊的任務,
fixedDelay
可能更為合適; - 對于需要在特定時間點或按照復雜時間規(guī)則執(zhí)行的任務,
cron
表達式則是不二之選。 initialDelay
參數可以幫助我們更好地控制任務的啟動時間,避免在應用啟動初期因大量任務同時啟動而帶來的性能壓力。
到此這篇關于Spring 定時任務@Scheduled 注解四大參數解析的文章就介紹到這了,更多相關Spring 定時任務@Scheduled 注解內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringCloud中NacosNamingService的作用詳解
這篇文章主要介紹了SpringCloud中NacosNamingService的作用詳解,NacosNamingService類完成服務實例注冊,撤銷與獲取服務實例操作,NacosNamingService初始化采用單例模式,使用反射生成,需要的朋友可以參考下2023-11-11