SpringBoot使用@Scheduled定時(shí)器的示例詳解
@Scheduled注解是 Spring Boot 提供的定時(shí)任務(wù)配置工具,用于控制任務(wù)執(zhí)行時(shí)間。
使用方法:
- 先啟用定時(shí)器配置,添加注解@EnableScheduling,可在@SpringBootApplication或者@Configuration或者@Component等spring能夠掃描到的地方;
- 在spring能夠識別的類中定義沒有返回值和參數(shù)的方法,在方法上使用@Scheduled注解;
@Scheduled注解的參數(shù)解釋:
- fixedRate:固定速率(ms),按照每次任務(wù)的開始時(shí)間間隔執(zhí)行(定時(shí)器啟動(dòng)的時(shí)候每次開始時(shí)間就已確定),上一次任務(wù)執(zhí)行時(shí)間超過后面的任務(wù)開始時(shí)間,后面任務(wù)會積壓到隊(duì)列里,等這個(gè)時(shí)間很長的任務(wù)結(jié)束后,隊(duì)列里的積壓任務(wù)全部同時(shí)放行;
- fixedRateString:同上,字符串類型,支持${}占位符,可靈活配置;
- fixedDelay:固定延遲(ms),以上次的結(jié)束時(shí)間開始延遲一定時(shí)間執(zhí)行下一次,不會積壓;
- fixedDelayString:同上,字符串類型,支持${}占位符,可靈活配置;
- initialDelay:延遲啟動(dòng)定時(shí)器時(shí)間(ms),指定一段時(shí)間后才開始啟動(dòng)定時(shí)器;
- initialDelayString:同上,字符串類型,支持${}占位符,可靈活配置;
- cron:cron表達(dá)式,比上面的更靈活,字符串類型,支持${}占位符,上次任務(wù)如果超時(shí)會自動(dòng)丟棄當(dāng)前時(shí)間應(yīng)該執(zhí)行的任務(wù);(跟linux的cron稍有區(qū)別,linux的是“分時(shí)日月周”,這個(gè)是“秒分時(shí)日月周[年]”,多了秒和可省略的年)
- zone:時(shí)區(qū),一般默認(rèn)即可,自動(dòng)使用服務(wù)器時(shí)區(qū),也可指定如“Asia/Shanghai”;
PS:占位符可以只占String的一部分;定時(shí)器里如果拋出異常不會影響下一次任務(wù);對cron的簡單理解,可以把每個(gè)字段的設(shè)置都解釋為一個(gè)整數(shù)數(shù)組,例如在秒字段上的0/10,可以解釋成[0, 10, 20, 30, 40, 50],在月字段上的*表示所有值,可解釋成[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],也就是每個(gè)月都有;
測試代碼:
啟動(dòng)類:
package testspringschedule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class TestSpringSchedule {
public static void main(String[] args) {
System.out.println("main start");
SpringApplication.run(TestSpringSchedule.class, args);
}
}測試fixedRate:
package testspringschedule;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
//@Configuration
@Component
//@EnableScheduling
public class MyRateSchedule {
@Scheduled(fixedRate = 5000, initialDelay = 3000, zone = "Asia/Shanghai")
// @Scheduled(fixedRateString = "5000", initialDelayString = "3000")
// @Scheduled(fixedRateString = "${abc:5}000", initialDelayString = "${def:3000}")
public void test1() {
System.out.println("fixedRate start:" + new Date());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("fixedRate stop:" + new Date());
}
private int i = 0;
// @Scheduled(fixedRate = 1000, initialDelay = 3000)
public void test2() {
System.out.println("fixedRate start:" + new Date());
i++;
System.out.println(i);
if (i == 5) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("fixedRate stop:" + new Date());
}
}test1()執(zhí)行結(jié)果,開始時(shí)間都是間隔5s:

test2()執(zhí)行結(jié)果,開始時(shí)間間隔1s,超時(shí)積壓多個(gè)任務(wù)等上一個(gè)任務(wù)結(jié)束后全部同時(shí)執(zhí)行;

測試fixedDelay:
package testspringschedule;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class MyDelaySchedule {
@Scheduled(fixedDelay = 5000, initialDelay = 3000)
public void test1() {
System.out.println("fixedDelay start:" + new Date());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("fixedDelay stop:" + new Date());
}
}執(zhí)行結(jié)果:

測試cron:
package testspringschedule;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class MyCronSchedule {
@Scheduled(cron = "50/2 * * * * ?")
// @Scheduled(cron = "${s:1/2 * * * * ?}")
// @Scheduled(cron = "${s:0/5} * * * * ?")
public void test1() {
System.out.println("cron:" + new Date());
}
// @Scheduled(cron = "0/10 * * * * ?")
public void test2() {
System.out.println("cron start:" + new Date());
try {
Thread.sleep(11000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("cron stop:" + new Date());
}
}test1()執(zhí)行結(jié)果:

test2()執(zhí)行結(jié)果,超時(shí)丟棄:

也可以實(shí)現(xiàn)SchedulingConfigurer接口,使用配置類進(jìn)行線程池、異常處理、拒絕策略等的一些配置:(默認(rèn)配置所有定時(shí)器都是單線程串行的)
package testspringschedule;
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.util.ErrorHandler;
@Configuration
public class MyScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
final ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(50);
taskScheduler.setThreadGroupName("Scheduled-Group");
taskScheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
taskScheduler.setErrorHandler(new ErrorHandler() {
@Override
public void handleError(Throwable t) {
System.out.println("發(fā)生未捕獲異常:");
t.printStackTrace();
}
});
taskScheduler.initialize();
taskRegistrar.setTaskScheduler(taskScheduler);
}
}到此這篇關(guān)于SpringBoot使用@Scheduled定時(shí)器的文章就介紹到這了,更多相關(guān)SpringBoot @Scheduled定時(shí)器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot使用定時(shí)器@Scheduled不管用的解決
- 使用springboot時(shí),解決@Scheduled定時(shí)器遇到的問題
- SpringBoot中@Scheduled實(shí)現(xiàn)服務(wù)啟動(dòng)時(shí)執(zhí)行一次
- 如何解決SpringBoot定時(shí)任務(wù)報(bào)錯(cuò)Unexpected error occurred in scheduled task問題
- SpringBoot使用@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)的并行執(zhí)行
- Springboot中@scheduled注解解析
- SpringBoot中@Scheduled()注解以及cron表達(dá)式詳解
- SpringBoot通過@Scheduled實(shí)現(xiàn)定時(shí)任務(wù)及單線程運(yùn)行問題解決
- SpringBoot?ScheduledTaskRegistrar解決動(dòng)態(tài)定時(shí)任務(wù)思路詳解
- 解決SpringBoot中的Scheduled單線程執(zhí)行問題
相關(guān)文章
Plugin ‘org.springframework.boot:spring-boot-maven-plug
這篇文章給大家介紹了Plugin ‘org.springframework.boot:spring-boot-maven-plugin:‘ not found的解決方案,親測可用,文中給出了兩種解決方法,需要的朋友可以參考下2024-01-01
springboot加載一個(gè)properties文件轉(zhuǎn)換為map方式
這篇文章主要介紹了springboot加載一個(gè)properties文件轉(zhuǎn)換為map方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Spring Boot 2.x 實(shí)現(xiàn)文件上傳功能
這篇文章主要介紹了Spring Boot 2.x 實(shí)現(xiàn)文件上傳功能,本文分步驟通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
idea使用外置tomcat配置springboot詳細(xì)步驟
昨天小編遇到一個(gè)問題使用springboot自帶的tomcat啟動(dòng)沒有任何問題,不知道idea使用外置tomcat配置springboot該如何設(shè)置的,今天小編給大家分享一篇教程幫助大家解決這個(gè)問題2021-07-07
詳解spring Boot 集成 Thymeleaf模板引擎實(shí)例
本篇文章主要介紹了spring Boot 集成 Thymeleaf模板引擎實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Spring使用@Retryable實(shí)現(xiàn)自動(dòng)重試機(jī)制
在微服務(wù)架構(gòu)中,服務(wù)之間的調(diào)用可能會因?yàn)橐恍簳r(shí)性的錯(cuò)誤而失敗,例如網(wǎng)絡(luò)波動(dòng)、數(shù)據(jù)庫連接超時(shí)或第三方服務(wù)不可用等,在本文中,我們將介紹如何在 Spring 中使用 @Retryable 實(shí)現(xiàn)自動(dòng)重試機(jī)制,需要的朋友可以參考下2025-01-01
java連接mysql數(shù)據(jù)庫詳細(xì)步驟解析
以下是對java連接mysql數(shù)據(jù)庫的具體詳細(xì)步驟進(jìn)行了分析介紹,需要的朋友可以過來參考下2013-08-08
如何實(shí)現(xiàn)在IDEA中導(dǎo)入一個(gè)模塊
這篇文章主要介紹了如何實(shí)現(xiàn)在IDEA中導(dǎo)入一個(gè)模塊方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04

