一 任務調度基本介紹
任務調度器就是按照規定的計划完成任務;比如windows,linux的自帶的任務調度系統功能;平常開發中也就是按照規定的時間點輪詢執行計划任務(比如每周三的凌晨進行數據備份),或者按時間隔觸發一次任務調度(比如每3小時執行一次定時抓拍);
二 corn表達式介紹
2.1 位數介紹
如果有用過quartz的讀者肯定了解cron時鍾周期計划;下面是cron對應位數的說明,其中第七位年份通常忽略,第四位跟第六位同時表達會有歧義,通常只表達具體的一位,另一位使用?表示解決沖突;
| 位數 | 說明 |
|---|---|
| 第一位 | second(0-59) |
| 第二位 | minute(0-59) |
| 第三位 | hour(0-23) |
| 第四位 | day of month(1-31) |
| 第五位 | month(1-12) |
| 第六位 | day of week(1-7)1是周日,7是周六 |
| 第七位 | year(1970-2099) |
2.2 占位符說明
| 占位符 | 說明 |
|---|---|
| * | 表示任意時刻 |
| ? | day of month 或者 day of week |
| - | 表示范圍 |
| / | 表示間隔 |
| , | 表示枚舉 |
| L | 表示最后day of month 或者 day of week |
| W | 表示有效工作日(1-5)day of month |
| # | 表示第幾個星期幾 day of week |
| LW | 表示某月最后一個工作日 |
2.3 常用cron舉例
| corn | 說明 |
|---|---|
| 0 0 3 * * ? | 每月每天凌晨3點觸發 |
| 0 0 3 1 * ? | 每月1日凌晨3點觸發 |
| 0 0 3 ? * WEN | 星期三中午12點觸發 |
| 0 0 3 ?* MON-FRI | 周一至周五凌晨3點觸發 |
| 0 0/5 8 * * ? | 每天7點至7:55分每隔5分鍾觸發一次 |
| 0 10,20 8 * * ? | 每天的8點10分,8點20分觸發 |
| 0 0 1-3 * * ? | 每天的1點至三點每小時觸發一次 |
| 0 0 8 L * ? | 每月最后一天的8點觸發 |
| 0 10 12 ? * 6#3 | 每月的第三個星期五的12:10分觸發 |
| 0 10 12 ? * 6L 2022 | 表示2022年每月最后一個星期五10:22分觸發 |
三使用cron進行任務調度
3.1 依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.2 具體corn任務調度計划
在@Scheduled注解中自定義cron調度計划;將注解用在需要進行調度的方法上
/**
* @Author lsc
* @Description <p> </p>
* @Date 2019/11/11 22:23
*/
@Service
public class PlainService {
@Scheduled(cron = "30 * * * * ?")
public void cronScheduled(){
System.out.println("關注作者博客和公眾號,不定期分享原創文章");
}
}
3.3 啟動類
啟動類需要加上 @EnableScheduling 表示開啟任務調度;
/**
* @Author lsc
* @Description <p> 任務調度啟動類 </p>
* @Date 2019/11/11 22:20
*/
@SpringBootApplication
// 開啟任務調度
@EnableScheduling
public class ScheduledApplication {
public static void main(String[] args) {
SpringApplication.run(ScheduledApplication.class,args);
}
}
四 Scheduled 中其它方式進行任務調度
4.1 fixedDelay
每隔3000毫秒執行一次,必須是上次調度成功后3000毫秒;
@Scheduled(fixedDelay = 3000)
public void fixedDelayScheduled(){
System.out.println("the day nice");
}
4.2fixedRate
每個3000毫秒執行一次,無論上次是否會執行成功,下次都會執行;
@Scheduled(fixedRate = 3000)
public void fixedRateScheduled(){
System.out.println("the night nice");
}
4.3 initialDelay
initialDelay 表示初始化延遲1000毫秒后,執行具體的任務調度,之后按照fixedRate進行任務調度;
@Scheduled(initialDelay = 1000,fixedRate = 3000)
public void initialDelayStringScheduled(){
System.out.println("the night nice");
}
五 作者相關
時光荏苒,初心不改,愛學習,愛每一天;源碼在github上

本套教程
- springboot入門 (1)
- Springboot自定義banner(2)
- springboot配置文件解析(3)
- springboot集成mybatis(4)
- springboot集成jdbcTemplate(5)
- spingboot單元測試(6)
- springboot集成thymeleaf(7)
- springboot多文件上傳(8)
- springboot文件下載(9)
- Springboot自定義異常類(10)
- springboot多環境配置(11)
- springboot自動配置原理解析(12)
- springboot集成restTemplate接口調用(13)
- springboot集成任務調度(14)
- springboot跨域CORS處理(15)
- springboot開啟GIZP壓縮(16)
- springboot集成logback(17)
- springboot集成Swagger(18)
- springboot集成actuator后台監控(19)
- springboot集成mybatis+oracle+druid(20)
- springboot 集成springsession(21)
- springboot集成jwt(22)
- springboot集成admin后台監控(23)
- springboot集成redis基礎篇(24)
- springboot集成redis緩存篇(25)
- springboot使用AOP日志攔截(26)
- springboot集成Validation參數校驗(27)
- springboot集成mybatisPlus(28)
- springboot集成shiro(29)
- springboot實現接口等冪次校驗(30)
- springboot-集成WebSockets(31)
- restTemplate源碼解析(32)
- SpringBoot使用@Async異步調用與線程池(33)
- 待續
