当前位置: 首页 > news >正文

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 个)由空格分隔的时间元素。从左至右,这些元素的定义如下:

  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 秒时

字段含义详解:

字符适用字段描述示例
*所有匹配每个值分钟字段 *:每分钟
-所有指定一个范围小时字段 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:周日之后

相关文章:

  • 动态规划简单题2
  • 博客打卡-人类基因序列功能问题动态规划
  • GESP2024年6月认证C++八级( 第二部分判断题(1-5))
  • 【现代深度学习技术】现代循环神经网络02:长短期记忆网络(LSTM)
  • GAMES202-高质量实时渲染(Real-time Environment Mapping)
  • Matlab/Simulink - BLDC直流无刷电机仿真基础教程(四) - PWM调制模拟
  • Spring AOP---面向切面编程由认识到使用
  • 自动化实现web端Google SignUp——selenium
  • 深入解析 Python 应用日志监控:ELK、Graylog 的实战指南
  • ​​​​​​​2025年第二十二届五一数学建模竞赛题目A题 支路车流量推测问题
  • 例数据中关键指标对应的SQL查询模板
  • 深度探索DeepSeek:从架构设计到性能优化的实战指南
  • 优雅关闭服务:深入理解 SIGINT / SIGTERM 信号处理机制
  • 装饰器模式深度解析:让对象功能扩展像乐高一样灵活 [特殊字符]
  • 0基础 | Proteus电路仿真 | 电机使用
  • 海量数据存储与分析:HBase vs ClickHouse vs Doris 三大数据库优劣对比指南
  • 《社交类应用开发:React Native与Flutter的抉择》
  • 【C语言】文本操作函数fseek、ftell、rewind
  • 装饰器设计模式(Decorator Pattern)详解
  • (A题|支路车流量推测问题)2025年第二十二届五一数学建模竞赛(五一杯/五一赛)解题思路|完整代码论文集合
  • 4月一二线城市新房价格环比上涨,沪杭涨幅居百城前列
  • 解放日报:让算力像“水电煤”赋能千行百业
  • 乌方公布矿产协议详情:未提债务义务,包含美再援助条款
  • 国铁集团郑州局预计“五一”发送642.5万人
  • 排除燃气爆炸、人为放火可能,辽宁辽阳火灾事故起火原因正在调查
  • 解放日报社论:只争朝夕、不负重托,加快建成具有全球影响力的科技创新高地