Spring Task 定时工作
次要内容
定时工作概述
在我的项目中开发定时工作应该一种比拟常见的需要,在 Java 中开发定时工作次要有三种解决方案:一是应用JDK 自带的 Timer,二是应用第三方组件 Quartz,三是应用 Spring Task。
Timer 是 JDK 自带的定时工作工具,其简略易用,然而对于简单的定时规定无奈满足,在理论我的项目开发中也很少应用到。Quartz 功能强大,然而应用起来绝对轻便。而 Spring Task 则具备前两者的长处(功能强大且简略易用),应用起来很简略,除 Spring 相干的包外不须要额定的包,而且反对注解和配置文件两种模式。
应用Spring Task实现定时工作
Spring Task 开发定时工作有两种工作配置形式:
1. XML 配置
2. 注解配置
应用 XML 配置实现定时工作
创立我的项目,增加依赖
创立Maven我的项目,在pom.xml配置文件中,批改我的项目环境,增加spring依赖坐标
<!-- 引入spring依赖坐标 --><dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.4.RELEASE</version></dependency>
增加配置文件 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>
定义定时工作办法
新建类,增加主动注入的注解,定义定义工作的办法
package com.xxxx.task;import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;import java.util.Date;@Componentpublic 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())); }}
定时工作命名空间的增加
在spring.xml配置文件中,增加定时工作的命名空间
xmlns:task="http://www.springframework.org/schema/task"http://www.springframework.org/schema/taskhttp://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">
定时工作配置
<!-- 配置定时规定 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>
测试定时工作
public static void main( String[] args ) { System.out.println("定义工作测试..."); // 获取Spring上下文环境 ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); // 获取指定Bean对象 TaskJob taskJob = (TaskJob) ac.getBean("taskJob");}
应用注解配置实现定时工作
定义定时工作办法
@Scheduled 注解的应用
package com.xxxx.task;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;import java.util.Date;@Componentpublic 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())); }}
定时工作命名空间的增加
在spring.xml配置文件中,增加定时工作的命名空间
xmlns:task="http://www.springframework.org/schema/task"http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task.xsd
配置定时工作驱动
<!-- 配置定时工作驱动。开启这个配置,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>
测试定时工作
public static void main( String[] args ) { System.out.println("定义工作测试..."); // 获取Spring上下文环境 ApplicationContext ac = new ClassPathXmlApplicationContext("spring.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-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
完结每 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