SpringBoot項目使用@Scheduled注解實現(xiàn)定時任務的方法
SpringBoot項目【使用@Scheduled注解實現(xiàn)定時任務】
使用SpringBoot創(chuàng)建定時任務目前主要有以下三種創(chuàng)建方式:
1、基于注解(@Scheduled)
- 最簡單直接
2、基于接口(SchedulingConfigurer)- 適于實際使用中從數(shù)據(jù)庫中讀取指定時間來動態(tài)執(zhí)行定時任務
3、基于注解設定多線程定時任務
1. 基于注解(@Scheduled)
1.1 @Scheduled 注解和 @EnableScheduling 注解的使用
基于注解@Scheduled默認為單線程,開啟多個任務時,任務的執(zhí)行會受上一個任務執(zhí)行時間影響
@EnableScheduling注解: 在配置類上使用,開啟計劃任務的支持(類上)。
@Scheduled注解: 來聲明這是一個任務,包括 cron,fixDelay,fixRate 等類型(方法上,需先開啟計劃任務的支持)。
【示例】SpringBoot項目中使用@Scheduled注解和@EnableScheduling注解實現(xiàn)定時任務。
(1)開啟定時任務
SpringBoot 項目在項目啟動類上添加 @EnableScheduling 注解即可開啟定時任務管理。
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); } }
(2)創(chuàng)建定時任務
創(chuàng)建定時任務,并使用 @Scheduled 注解。
package com.pjb.Schedule; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; /** * 定時任務的使用 * @author pan_junbiao **/ @Component public class Task { @Scheduled(cron="0/5 * * * * ? ") //每5秒執(zhí)行一次 public void execute(){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //設置日期格式 System.out.println("歡迎訪問 pan_junbiao的博客 " + df.format(new Date())); } }
1.2 @Scheduled 注解各參數(shù)講解
@Scheduled
注解具有多個參數(shù),以下是常用的幾個參數(shù):
cron
接收一個 cron 表達式。cron 表達式由六個或七個域組成,常見的格式如下:
css
復制編輯
[秒] [分] [小時] [日] [月] [周] [年]
序號 | 說明 | 是否必填 | 允許填寫的值 | 允許的通配符 |
---|---|---|---|---|
1 | 秒 | 是 | 0-59 | , - * / |
2 | 分 | 是 | 0-59 | , - * / |
3 | 小時 | 是 | 0-23 | , - * / |
4 | 日 | 是 | 1-31 | , - * ? / L W |
5 | 月 | 是 | 1-12 or JAN-DEC | , - * / |
6 | 周 | 是 | 1-7 or SUN-SAT | , - * ? / L # |
7 | 年 | 否 | 空或1970-2099 | , - * / |
通配符說明:
? 表示不指定值。使用的場景為不需要關心當前設置這個字段的值。例如:要在每月的10號觸發(fā)一個操作,但不關心是周幾,所以需要周位置的那個字段設置為"?" 具體設置為 0 0 0 10 * ?
-表示區(qū)間。例如 在小時上設置 “10-12”,表示 10,11,12點都會觸發(fā)。
, 表示指定多個值,例如在周字段上設置 “MON,WED,FRI” 表示周一,周三和周五觸發(fā)
/ 用于遞增觸發(fā)。如在秒上面設置"5/15" 表示從5秒開始,每增15秒觸發(fā)(5,20,35,50)。在月字段上設置’1/3’所示每月1號開始,每隔三天觸發(fā)一次。
L 表示最后的意思。在日字段設置上,表示當月的最后一天(依據(jù)當前月份,如果是二月還會依據(jù)是否是潤年[leap]), 在周字段上表示星期六,相當于"7"或"SAT"。如果在"L"前加上數(shù)字,則表示該數(shù)據(jù)的最后一個。例如在周字段上設置"6L"這樣的格式,則表示“本月最后一個星期五"
W 表示離指定日期的最近那個工作日(周一至周五). 例如在日字段上設置"15W",表示離每月15號最近的那個工作日觸發(fā)。(注,“W"前只能設置具體的數(shù)字,不允許區(qū)間”-")
常用示例:
定時任務表達式 | 描述 |
---|---|
0 0 12 * * ? | 每天12點觸發(fā) |
0 15 10 ? * * | 每天10點15分觸發(fā) |
0 15 10 * * ? | 每天10點15分觸發(fā) |
0 15 10 * * ? * | 每天10點15分觸發(fā) |
0 15 10 * * ? 2025 | 2025年每天10點15分觸發(fā) |
0 * 14 * * ? | 每天下午的 2點到2點59分每分觸發(fā) |
0 0/5 14 * * ? | 每天下午的 2點到2點59分(整點開始,每隔5分觸發(fā)) |
0 0/5 14,18 * * ? | 每天下午的 2點到2點59分(整點開始,每隔5分觸發(fā))每天下午的 18點到18點59分(整點開始,每隔5分觸發(fā)) |
0 0-5 14 * * ? | 每天下午的 2點到2點05分每分觸發(fā) |
0 10,44 14 ? 3 WED | 3月分每周三下午的 2點10分和2點44分觸發(fā) |
0 15 10 ? * MON-FRI | 從周一到周五每天上午的10點15分觸發(fā) |
0 15 10 15 * ? | 每月15號上午10點15分觸發(fā) |
0 15 10 L * ? | 每月最后一天的10點15分觸發(fā) |
0 15 10 ? * 6L | 每月最后一周的星期五的10點15分觸發(fā) |
0 15 10 ? * 6L 2022-2025 | 從2022年到2025年每月最后一周的星期五的10點15分觸發(fā) |
0 15 10 ? * 6#3 | 每月的第三周的星期五開始觸發(fā) |
0 0 12 1/5 * ? | 每月的第一個中午開始每隔5天觸發(fā)一次 |
0 11 11 11 11 ? | 每年的11月11號 11點11分觸發(fā)(光棍節(jié)) |
其他常用
- fixedDelay: 上次任務完成后多少毫秒再執(zhí)行。
- fixedRate: 上次任務開始后多少毫秒再執(zhí)行。
- initialDelay: 第一次延遲多少毫秒后再執(zhí)行。
2. 動態(tài)定時任務:基于接口(SchedulingConfigurer)
2.1 創(chuàng)建數(shù)據(jù)庫表
在 MySQL 數(shù)據(jù)庫中創(chuàng)建一個 cron 表,存儲定時任務的 cron 表達式:
DROP TABLE IF EXISTS cron; CREATE TABLE cron ( cron_id VARCHAR(30) NOT NULL PRIMARY KEY, cron VARCHAR(30) NOT NULL ); INSERT INTO cron VALUES ('1', '0/5 * * * * ?');
2.2 添加 MyBatis 依賴
在 pom.xml
中添加 MyBatis 和 MySQL 的 JDBC 依賴:
<!-- MyBatis與SpringBoot整合依賴 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <!-- MySQL的JDBC數(shù)據(jù)庫驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency>
2.3 創(chuàng)建定時任務配置類
package com.pjb.config; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.support.CronTrigger; import org.springframework.util.StringUtils; import java.time.LocalDateTime; /** * 動態(tài)定時任務配置類 * @author pan_junbiao **/ @Configuration @EnableScheduling public class DynamicScheduleConfigurer implements SchedulingConfigurer { @Mapper public interface CronMapper { @Select("select cron from cron limit 1") public String getCron(); } @Autowired CronMapper cronMapper; @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask( () -> System.out.println("歡迎訪問 pan_junbiao的博客: " + LocalDateTime.now().toLocalTime()), triggerContext -> { String cron = cronMapper.getCron(); if (StringUtils.isEmpty(cron)) { // 錯誤處理 } return new CronTrigger(cron).nextExecutionTime(triggerContext); } ); } }
3. 基于注解設定多線程定時任務
通過 @Async
注解開啟多線程定時任務,解決多個任務執(zhí)行時相互影響的問題。
創(chuàng)建多線程定時任務
package com.pjb.Task; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.time.LocalDateTime; /** * 基于注解設定多線程定時任務 * @author pan_junbiao */ @Component @EnableScheduling // 開啟定時任務 @EnableAsync // 開啟多線程 public class MultithreadScheduleTask { @Async @Scheduled(fixedDelay = 1000) // 間隔1秒 public void first() throws InterruptedException { System.out.println("第一個定時任務開始 : " + LocalDateTime.now().toLocalTime() + "\r\n線程 : " + Thread.currentThread().getName()); Thread.sleep(1000 * 10); } @Async @Scheduled(fixedDelay = 2000) public void second() { System.out.println("第二個定時任務開始 : " + LocalDateTime.now().toLocalTime() + "\r\n線程 : " + Thread.currentThread().getName()); } }
注意
由于 @Scheduled
默認是單線程模式,開啟多個定時任務時任務的執(zhí)行順序會受前一個任務的執(zhí)行時間影響。通過 @Async
注解,我們可以解決這個問題,使多個定時任務并行執(zhí)行。
通過以上三種方式,Spring Boot 提供了靈活的定時任務支持,可以根據(jù)項目需求選擇合適的實現(xiàn)方式。
到此這篇關于SpringBoot項目使用@Scheduled注解實現(xiàn)定時任務的文章就介紹到這了,更多相關SpringBoot @Scheduled定時任務內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring JPA聯(lián)表查詢之OneToOne源碼詳解
這篇文章主要為大家介紹了Spring JPA聯(lián)表查詢之OneToOne源碼詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04Springboot整合JwtHelper實現(xiàn)非對稱加密
本文主要介紹了Springboot整合JwtHelper實現(xiàn)非對稱加密,主要介紹兩種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-03-03SpringBoot ThreadLocal 簡單介紹及使用詳解
ThreadLocal 叫做線程變量,意思是 ThreadLocal 中填充的變量屬于當前線程,該變量對其他線程而言是隔離的,也就是說該變量是當前線程獨有的變量,這篇文章主要介紹了SpringBoot ThreadLocal 的詳解,需要的朋友可以參考下2024-01-01Springboot集成Spring Security實現(xiàn)JWT認證的步驟詳解
這篇文章主要介紹了Springboot集成Spring Security實現(xiàn)JWT認證的步驟詳解,幫助大家更好的理解和使用springboot,感興趣的朋友可以了解下2021-02-02