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

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:周日之后
http://www.xdnf.cn/news/3345.html

相关文章:

  • GPU虚拟化实现(七)
  • MySQL基础关键_003_DQL(二)
  • 动态规划简单题
  • 【验证技能】验证质量活动及其执行注意事项
  • 权限提升—Linux提权内核溢出漏洞辅助项目
  • 【QNX+Android虚拟化方案】138 - USB 底层传输原理
  • 实验五 完整性
  • 初学者如何学习AI问答应用开发范式
  • PostgreSQL数据类型
  • 使用Python和Pandas实现的Amazon Redshift权限检查与SQL生成用于IT审计
  • 【DeepMLF】具有可学习标记的多模态语言模型,用于情感分析中的深度融合
  • EBO的使用
  • 基于python的人工智能应用简述
  • Spring 提供了多种依赖注入的方式
  • C#泛型集合深度解析(九):掌握System.Collections.Generic的核心精髓
  • 电池预测 | 第27讲 基于CNN卷积神经网络的锂电池剩余寿命预测
  • x86架构详解:定义、应用及特点
  • C++/SDL 进阶游戏开发 —— 双人塔防(代号:村庄保卫战 18)
  • 人工智能对未来工作的影响
  • 治理和管理的区别
  • Linux内核notify通知笔录
  • 软件测评中心如何保障软件质量与性能?评测范围和标准有哪些?
  • Java 多线程进阶:线程安全、synchronized、死锁、wait/notify 全解析(含代码示例)
  • Go 语言中一个功能强大且广泛使用的数据验证库github.com/go-playground/validator/v10
  • 2025五一杯数学建模A题:支路车流量推测问题,思路分析+模型代码
  • 拉宾公钥密码算法实现
  • 面经-计算机网络——OSI七层模型与TCP/IP四层模型的对比详解
  • IDEA在项目中添加模块出现Error adding module to project: null(向项目添加模块时出错: null)的解决方法
  • 位运算切换大小写
  • 数字智慧方案6158丨智慧医疗解决方案精华版(58页PPT)(文末有下载方式)