SpringTas定时任务使用详解
文章目录
- 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:周日之后 |