Spring Task 定时任务
1. 主要内容(了解)
2. 定时任务概述(了解)
在项⽬中开发定时任务应该⼀种⽐较常⻅的需求,在 Java 中开发定时任务主要有三种解决⽅案:⼀是使⽤JDK ⾃带的 Timer,⼆是使⽤第三⽅组件 Quartz,三是使⽤ Spring Task。
Timer 是 JDK ⾃带的定时任务⼯具,其简单易⽤,但是对于复杂的定时规则⽆法满⾜,在实际项⽬开发中也很少使⽤到。Quartz 功能强⼤,但是使⽤起来相对笨重。⽽ Spring Task 则具备前两者的优点(功能强⼤且简单易⽤),使⽤起来很简单,除 Spring 相关的包外不需要额外的包,⽽且⽀持注解和配置⽂件两种形式。
3. 使⽤Spring Task实现定时任
1. XML 配置
2.注解配置
3.1. 使⽤ XML 配置实现定时任务(了解)
3.1.1. 创建项⽬,添加依赖
创建Maven项⽬,在pom.xml配置⽂件中,修改项⽬环境,添加spring依赖坐标
<!-- 引⼊spring依赖坐标 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
3.1.2. 添加配置⽂件 spring.xml
在src/main/resources⽬录下新建配置⽂件spring.xml,并设置Spring扫描器
<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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"><!-- Spring扫描注解的配置 -->
<context:component-scan base-package="com.xxxx" />
</beans>
3.1.3. 定义定时任务⽅法
新建类,添加⾃动注⼊的注解,定义定义任务的⽅法
package com.xxxx.task;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class TaskJob {
public void job1(){
System.out.println("任务 1:" +
new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
}
public void job2(){
System.out.println("任务 2:" +
new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
}
}
3.1.4. 定时任务命名空间的添加
在spring.xml配置⽂件中,添加定时任务的命名空间
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
修改配置⽂件内容如下:
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
3.1.5. 定时任务配置
<!--
配置定时规则
ref:指定的类,即任务类
method:指定的即需要运⾏的⽅法
cron:cronExpression表达式
-->
<task:scheduled-tasks>
<!-- 每个两秒执⾏⼀次任务 -->
<task:scheduled ref="taskJob" method="job1" cron="0/2 * * * * ?"/>
<!-- 每隔五秒执⾏⼀次任务 -->
<task:scheduled ref="taskJob" method="job2" cron="0/5 * * * * ?"/>
<!-- 多个定时任务 在这⾥配置 -->
</task:scheduled-tasks>
3.1.6. 测试定时任务
public static void main( String[] args ) {
System.out.println("定义任务测试...");
// 获取Spring上下⽂环境
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); // 获取指定Bean对象
TaskJob taskJob = (TaskJob) ac.getBean("taskJob");
}
3.2. 使⽤注解配置实现定时任务(操作)
3.2.1. 定义定时任务⽅法
@Scheduled 注解的使⽤
package com.xxxx.task;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class TaskJob02 {
@Scheduled(cron="0/2 * * * * ?")
public void job1(){
System.out.println("任务 1:" +
new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
}
@Scheduled(cron="0/5 * * * * ?")
public void job2(){
System.out.println("任务 2:" +
new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
}
}
3.2.2. 定时任务命名空间的添加
在spring.xml配置⽂件中,添加定时任务的命名空间
xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
3.2.3. 配置定时任务驱动
<!-- 配置定时任务驱动。开启这个配置,spring才能识别@Scheduled注解 -->
<task:annotation-driven />
配置⽂件内容如下:
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<!-- Spring扫描注解的配置 -->
<context:component-scan base-package="com.xxxx" />
<!-- 配置定时任务驱动。开启这个配置,spring才能识别@Scheduled注解 --> <task:annotation-driven />
</beans>
3.2.4. 测试定时任务
public static void main( String[] args ) {
System.out.println("定义任务测试...");
// 获取Spring上下⽂环境
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); // 获取指定Bean对象
TaskJob02 taskJob02 = (TaskJob02) ac.getBean("taskJob02");
}
4. Cron 表达式简介(了解)
关于 cronExpression 表达式有⾄少 6 个(也可能是 7 个)由空格分隔的时间元素。从左⾄右,这些元素的定义如下:
1.秒(0–59)
2.分钟(0–59)
3.⼩时(0–23)
4.⽉份中的⽇期(1–31)
5.⽉份(1–12 或 JAN–DEC)
6.星期中的⽇期(1–7 或 SUN–SAT)
7.年份(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 秒时
各个时间可⽤值如下:
秒 0-59 , - * /
分 0-59 , - * /
⼩时 0-23 , - * /
⽇ 1-31 , - * ? / L W C
⽉ 1-12 or JAN-DEC , - * /
周⼏ 1-7 or SUN-SAT , - * ? / L C #
年(可选字段) empty, 1970-2099 , - * /
可⽤值详细分析如下:
"*" —— 字符可以⽤于所有字段,在"分"字段中设为"*",表示"每⼀分钟"的含义。
"?" —— 字符可以⽤在"⽇"和"周⼏"字段,它⽤来指定"不明确的值"。
这在你需要指定这两个字段中的某⼀个值⽽不是另外⼀个的时候会被⽤到。在后⾯的例⼦中可以看到其含义。"-" —— 字符被⽤来指定⼀个值的范。
⽐如在"⼩时"字段中设为"10-12",表示"10 点到 12 点"。
"," —— 字符指定数个值。
⽐如在"周⼏"字段中设为"MON,WED,FRI",表示"the days Monday, Wednesday, and Friday"。 "/" —— 字符⽤来指定⼀个值的的增加幅度。
⽐如在"秒"字段中设置为"0/15"表示"第 0, 15, 30,和 45 秒"。
⽽"5/15"则表示"第 5, 20, 35,和 50"。
在'/'前加"*"字符相当于指定从 0 秒开始。每个字段都有⼀系列可以开始或结束的数值。 对于"秒"和"分"字段来说,其数值范围为 0 到 59。
对于"⼩时"字段来说其为 0 到 23,对于“⽇”字段来说为 0 到 31。
⽽对于"⽉"字段来说为 1 到 12。
"/"字段仅仅只是帮助你在允许的数值范围内从开始"第 n"的值。
"L" —— 字符可⽤在"⽇"和"周⼏"这两个字段。它是"last"的缩写,但是在这两个字段中有不同的含义。 "⽇"字段中的"L"表示"⼀个⽉中的最后⼀天",对于⼀⽉就是 31 号,对于⼆⽉来说就是 28 号(⾮闰年)。 "周⼏"字段中,它简单的表示"7" or "SAT"。
但是如果在"周⼏"字段中使⽤时跟在某个数字之后,它表示"该⽉最后⼀个星期×"。
⽐如"6L"表示"该⽉最后⼀个周五"。
当使⽤"L"选项时,指定确定的列表或者范围⾮常重要,否则你会被结果搞糊涂的。
"W" —— 可⽤于"⽇"字段。⽤来指定历给定⽇期最近的⼯作⽇(周⼀到周五)。
⽐如将"⽇"字段设为"15W",意为: "离该⽉ 15 号最近的⼯作⽇"。
因此如果 15 号为周六,触发器会在 14 号即周五调⽤。
如果 15 号为周⽇,触发器会在 16 号也就是周⼀触发。如果 15 号为周⼆,那么当天就会触发。 如果"⽇"字段设为"1W",⽽⼀号是周六,会于下周⼀即当⽉的 3 号触发,它不会越过当⽉的值的范围边界。 "W"字符只能⽤于"⽇"字段的值为单独的⼀天⽽不是⼀系列值的时候。
"L"和"W"可以组合⽤于“⽇”字段表示为'LW',意为"该⽉最后⼀个⼯作⽇"。
"#" —— 字符可⽤于"周⼏"字段。该字符表示"该⽉第⼏个周×"。
⽐如"6#3"表示该⽉第三个周五( 6 表示周五,⽽"#3"该⽉第三个)。 再⽐如: "2#1" 表示该⽉第⼀个周⼀,⽽"4#5" 该⽉第五个周三。
注意如果你指定"#5"该⽉没有第五个"周×",该⽉是不会触发的。
"C" —— 字符可⽤于"⽇"和"周⼏"字段,它是"calendar"的缩写。
它表示为基于相关的⽇历所计算出的值(如果有)。如果没有关联的⽇历,那它等同于包含全部⽇历。 "⽇"字段值为"5C",表示"⽇历中的第⼀天或者 5 号以后"。
"周⼏"字段值为"1C",则表示"⽇历中的第⼀天或者周⽇以后"。
对于"⽉份"字段和"周⼏"字段来说合法的字符都不是⼤⼩写敏感的。
⼀些例⼦:
"0 0 12 * * ?" 每天中午⼗⼆点触发
"0 15 10 ? * *" 每天早上 10:15 触发
"0 15 10 * * ?" 每天早上 10:15 触发
"0 15 10 * * ? *" 每天早上 10:15 触发
"0 15 10 * * ? 2005" 2005 年的每天早上 10:15 触发
"0 * 14 * * ?" 每天从下午 2 点开始到 2 点 59 分每分钟⼀次触发
"0 0/5 14 * * ?" 每天从下午 2 点开始到 2:55 分结束每 5 分钟⼀次触发
"0 0/5 14,18 * * ?" 每天的下午 2 点⾄ 2:55 和 6 点⾄ 6 点 55 分两个时间段内每 5分钟⼀次触发
"0 0-5 14 * * ?" 每天 14:00 ⾄ 14:05 每分钟⼀次触发
"0 10,44 14 ? 3 WED" 三⽉的每周三的 14:10 和 14:44 触发
"0 15 10 ? * MON-FRI" 每个周⼀、周⼆、周三、周四、周五的 10:15 触 发
"0 15 10 15 * ?" 每⽉ 15 号的 10:15 触发
"0 15 10 L * ?" 每⽉的最后⼀天的 10:15 触发
"0 15 10 ? * 6L" 每⽉最后⼀个周五的 10:15