網站首頁 編程語言 正文
1:在Spring Boot的啟動類上添加@EnableScheduling注解啟用定時任務
2:在類上添加@Component注解,定時任務方法上添加@Scheduled注解
@Component
public class BatchNumberTask {
/**
* 每天24:00定時執行
*/
@Scheduled(cron = "0 0 0 * * ? ")
public void batchNumber(){
//你的業務邏輯代碼
}
}
3:cron表達式在線生成器
@Scheduled()注解以及cron表達式詳解
@Scheduled注解:任務自動化調度。
@Scheduled(fixedDelay = 5000)
延遲執行。任務在上個任務完成后達到設置的延時時間就執行。
此處,任務會在上個任務完成后經過5s再執行。
@Scheduled(fixedRate = 5000)
定時執行。任務間隔規定時間即執行。
此處,任務每隔五秒便會執行一次。
@Scheduled(cron = “0 0 2 * * ?”)
自定義規則執行。比較復雜,一般用6或7個標識符(非官方叫法,方便區分記憶這么叫的)表示任務的執行規則。
此處的邏輯是每天的凌晨兩點開始定時執行任務。
補充:cron表達式
例1:@scheduled (cron=“0 0 2 2 * ? *”) 表示:每月二號凌晨兩點執行。
例2:@scheduled (cron=“15,20,40 15-20 2/5 L * ?”) 表示:每月的最后一天的凌晨2點開始每隔5小時的第15分鐘到第20分鐘之間的每分鐘的第15,20,40秒執行一次。
例3:@scheduled (cron=“0 0 0 15W * ?”) 表示:每月離15號最近的工作日。
例4:@scheduled (cron= "0 0 0 0 * 2#3 ") 表示:每月的第三個周一。
原文鏈接:https://blog.csdn.net/lixuanhong/article/details/127245878
相關推薦
- 2023-07-09 Golang 列表list
- 2023-07-08 vscode上查看git的記錄,可以看到是誰多久前修改的代碼
- 2022-06-11 在Docker容器中部署MSSQL_docker
- 2023-05-30 Python賦值邏輯的實現_python
- 2023-09-12 spring webflux配置成tomcat的線程池
- 2022-08-06 Python?pandas庫中isnull函數使用方法_python
- 2022-07-06 C#中的反射(System.Reflection)_C#教程
- 2022-12-05 Android?RecyclerView緩存復用原理解析_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支