Springboot如何配置Scheduler定時(shí)器
更新時(shí)間:2025年03月22日 10:55:37 作者:承文全
這篇文章主要介紹了Springboot如何配置Scheduler定時(shí)器問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
Springboot配置Scheduler定時(shí)器
1.在啟動(dòng)類上添加 @EnableScheduling 注解
開啟定時(shí)器
2.設(shè)置定時(shí)器任務(wù)(間隔和cron表達(dá)式都行)
package com.example.springboot01.config; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class MyScheduler { @Scheduled(fixedRate = 1000) //每2秒執(zhí)行一次 public void statusCheck() { System.out.println("【*** A ***】 間隔調(diào)度"); } @Scheduled(cron="* * * * * ?") //每秒調(diào)用一次 public void removeToHis() { try { //睡眠3秒 Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("【*** B ***】cron調(diào)度"); } }
3.配置調(diào)度池
package com.example.springboot01.config; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import java.util.concurrent.Executors; /** * 用于做并行調(diào)度使用 */ @Configuration public class SchedulerConfig implements SchedulingConfigurer { @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(100)); } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中關(guān)鍵字final finally finalize的區(qū)別介紹
這篇文章主要給大家分享的是 Java中final,finally,finalize 到底有什么區(qū)別,文章圍繞final,finally,finalize的相關(guān)資料展開詳細(xì)內(nèi)容,具有一定的參考的價(jià)值,需要的朋友可以參考一下2022-04-04JAVA 獲取系統(tǒng)當(dāng)前時(shí)間實(shí)例代碼
這篇文章主要介紹了JAVA 獲取系統(tǒng)當(dāng)前時(shí)間實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10Java微信公眾平臺(tái)之群發(fā)接口(高級(jí)群發(fā))
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺(tái)之群發(fā)接口,高級(jí)群發(fā)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

關(guān)于Java中攔截mybatis并輸出完整sql語句的方法
這篇文章主要介紹了關(guān)于Java中攔截mybatis并輸出完整sql語句的方法,假如項(xiàng)目中有很多很多的SQL我們不可能一一的去修改解決。這個(gè)時(shí)候我們就需要通過mybatis攔截SQL并且最終修改SQL,需要的朋友可以參考下
2023-08-08 
Java使用路徑通配符加載Resource與profiles配置使用詳解
這篇文章主要介紹了Java使用路徑通配符加載Resource與profiles配置使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
2020-06-06 
Java 1.8使用數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列
這篇文章主要為大家詳細(xì)介紹了Java 1.8使用數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
2020-10-10