Spring Task 定时任务
一、使用 XML 配置实现定时任务
添加spring坐标依赖。
1.1 定义定时任务方法
新建类,添加⾃动注⼊的注解,定义定义任务的⽅法
/**
* 任意类@Component
*/
@Component
public class TaskJob {
public void job01(){
System.out.println("任务 1:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
}
public void job02(){
System.out.println("任务 2:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
}
}
1.2 添加配置⽂件 spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.shsxt" />
<!--
配置定时规则
ref:指定的类,即任务类
method:指定的即需要运⾏的⽅法
cron:cronExpression表达式
-->
<task:scheduled-tasks>
<!-- 每个两秒执执行一次任务 -->
<task:scheduled ref="taskJob" method="job01" cron="0/2 * * * * ?"/>
<!-- 每隔五秒执行一次任务 -->
<task:scheduled ref="taskJob" method="job02" cron="0/5 * * * * ?"/>
<!-- 多个定时任务 在这里配置 -->
</task:scheduled-tasks>
</beans>
1.3 测试定时任务
public class Test01 {
public static void main(String[] args) {
System.out.println("定义任务测试...");
// 获取Spring上下文环境
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
// 获取指定Bean对象
TaskJob taskJob = (TaskJob) ac.getBean("taskJob");
}
}
二、使用注解配置实现定时任务
2.1 定义定时任务方法
/**
* 任意类@Component
*/
@Component
public class TaskJob02 {
@Scheduled(cron = "0/2 * * * * ?")
public void job01(){
System.out.println("任务 1:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
}
@Scheduled(cron = "0/5 * * * * ?")
public void job02(){
System.out.println("任务 2:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
}
}
2.2 添加配置⽂件 spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.shsxt" />
<!-- 配置定时任务驱动。开启这个配置,spring才能识别@Scheduled注解 -->
<task:annotation-driven />
</beans>
2.3 测试定时任务
public class Test02 {
public static void main(String[] args) {
System.out.println("定义任务测试...");
// 获取Spring上下文环境
ApplicationContext ac = new ClassPathXmlApplicationContext("spring02.xml");
// 获取指定Bean对象
TaskJob02 taskJob02 = (TaskJob02) ac.getBean("taskJob02");
}
}
三、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 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``