文章目錄
- Spring Task概述
- 1、環境配置
- 2.注解實現定時任務
- 2.注解實現定時任務
- 4. cron表達式詳解:
Spring Task概述
在開發中,我們經常會用到定時任務,而Spring Task 則是Spring提供的定時任務框架。
其它定時任務實現框架又jdk自帶Timer和Quartz,前者功能太簡單不經常用,后者太笨重,使用起來很重。而Spring Task 則是結合了兩者優缺點,同時因為是Spring自帶的,和Spring兼容性特別好,支持xml和注解兩種配置方式。
1、環境配置
添加pom.xml依賴:
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.4.RELEASE</version></dependency></dependencies>
2.注解實現定時任務
添加定時任務類
@Component
public class TaskJob {private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 定義定時任務的方法public void job1() {System.out.println("任務1:" + simpleDateFormat.format(new Date()));}}
項目結構:
添加spring.xml:
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:task="http://www.springframework.org/schema/task"xmlns:bean="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task.xsd"><!-- 開啟自動掃描 --><bean:component-scan base-package="com.xxxx"/><!-- 配置定時任務規則 --><task:scheduled-tasks><!-- 可以配置多個定時任務 --><!-- 定時任務1 --><task:scheduled ref="taskJob" method="job1" cron="0/2 * * * * ?"/></task:scheduled-tasks></beans>
測試類:
public class Test {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");TaskJob taskJob = (TaskJob) ac.getBean("taskJob");taskJob.job1();}
}
運行結果:
任務1:2025-05-01 13:33:15
任務1:2025-05-01 13:33:16
任務1:2025-05-01 13:33:18
任務1:2025-05-01 13:33:20
任務1:2025-05-01 13:33:22
.......
可以看到每兩秒執行一次。
2.注解實現定時任務
其實就是通過@Scheduled注解實現。
spring.xml配置:
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:task="http://www.springframework.org/schema/task"xmlns:bean="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task.xsd"><!-- 開啟自動掃描 --><bean:component-scan base-package="com.xxxx"/><!-- 配置定時任務注解驅動 --><task:annotation-driven/>
</beans>
定時任務類配置:
@Component
public class TaskJob02 {private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@Scheduled(cron = "0/2 * * * * ?")// 定義定時任務的方法public void job1() {System.out.println("任務1:" + simpleDateFormat.format(new Date()));}
}
測試類內容:
ApplicationContext ac = new ClassPathXmlApplicationContext("spring02.xml");
TaskJob02 taskJob02 = (TaskJob02) ac.getBean("taskJob02");
taskJob02.job1();
運行結果:
任務1:2025-05-01 13:44:27
任務1:2025-05-01 13:44:28
任務1:2025-05-01 13:44:30
任務1:2025-05-01 13:44:32
任務1:2025-05-01 13:44:34
......
4. cron表達式詳解:
Cron表達式是一種用于指定定時任務的時間表達式,常用來指定任務的執行時間、執行頻率和執行間隔。它由6~7個字段組成,分別表示秒、分、時、日期、月份、星期、年份(可省略)。cron 是一個非常靈活和強大的工具,幾乎可以用于任何需要定期執行的任務。
這里有一個可以自動生成cron表達式的網站:
自動生成cron表達式
關于cronExpression 表達式至少有 6 個(也可能是 7 個)由空格分隔的時間元素。從左至右,這些元素的定義如下:
- 秒 (0-59)
- 分鐘 (0-59)
- 小時 (0-23)
- 月份中的日期 (1-31)
- 月份 (1-12 或 JAN-DEC)
- 星期中的日期 (1-7 或 SUN-SAT)
- 年份 (1970-2099)
0 0 10,14,16 * * ?每天上午 10 點,下午 2 點和下午 4 點0 0,15,30,45 * 1-10 * ?每月前 10 天每隔 15 分鐘30 0 0 1 1 ? 2012從 2012 年 1 月 1 日午夜初 30 秒時
字段含義詳解:
字符 | 適用字段 | 描述 | 示例 |
---|---|---|---|
* | 所有 | 匹配每個值 | 分鐘字段 *:每分鐘 |
- | 所有 | 指定一個范圍 | 小時字段 10-12:10、11、12點 |
, | 所有 | 指定多個離散值 | 周幾字段 MON,WED,FRI:周一、周三、周五 |
/ | 所有 | 指定增量(步長值) | 秒字段 0/15:0、15、30、45秒 |
L | 日期字段、周幾字段 | 在日期字段:當月的最后一天;在周幾字段:該月該周幾的最后一次出現 | 日期字段 L:最后一天;周幾字段 5L:最后一個周五 |
W | 日期字段 | 指定日期最近的工作日(周一到周五) | 15W:15號最近的工作日 |
# | 周幾字段 | 指定該月該周幾的第幾次出現 | 5#3:第三次周五 |
c | 日期字段、周幾字段 | 基于日歷;指日歷中的第一天或指定值之后 | 日期字段 5c:5號之后;周幾字段 1c:周日之后 |