Springboot如何配置Scheduler定時器
更新時間:2025年03月22日 10:55:37 作者:承文全
這篇文章主要介紹了Springboot如何配置Scheduler定時器問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Springboot配置Scheduler定時器
1.在啟動類上添加 @EnableScheduling 注解
開啟定時器
2.設置定時器任務(間隔和cron表達式都行)
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 ***】 間隔調度");
}
@Scheduled(cron="* * * * * ?") //每秒調用一次
public void removeToHis() {
try {
//睡眠3秒
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("【*** B ***】cron調度");
}
}3.配置調度池
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;
/**
* 用于做并行調度使用
*/
@Configuration
public class SchedulerConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(100));
}
}總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java中關鍵字final finally finalize的區(qū)別介紹
這篇文章主要給大家分享的是 Java中final,finally,finalize 到底有什么區(qū)別,文章圍繞final,finally,finalize的相關資料展開詳細內(nèi)容,具有一定的參考的價值,需要的朋友可以參考一下2022-04-04
Java使用路徑通配符加載Resource與profiles配置使用詳解
這篇文章主要介紹了Java使用路徑通配符加載Resource與profiles配置使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
Java 1.8使用數(shù)組實現(xiàn)循環(huán)隊列
這篇文章主要為大家詳細介紹了Java 1.8使用數(shù)組實現(xiàn)循環(huán)隊列,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-10-10

