1. 前言
上一篇我们学习了Quartz作为定时任务的框架的使用, 这一篇我们来学习Spring全家桶的SpringTask, 对于主张简单易用的Spring家族来说, SpringTask无疑也是一个轻量级的框架,他比Quartz更容易上手.
2. pom.xml依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
</dependencies>
对的,一个spring-context就是他所需要的框架了, 也就是说, 如果你现在开发的项目是用的spring ,无需管依赖,直接看下一步就可以了.
2. 实现代码
spring 的老套路, 一般都可以用两种方式实现: 一是配置方式,二是注解方式
2.1 基于xml配置文件的方式
1 . 写一个运行任务的job类
package com.zgd.spring.task;
import java.util.Date;
/**
* xml配置方式的定时任务
*/
public class XmlTask {
/**
* 要执行的任务
*/
public void task(){
System.out.println("定时任务执行中: "+new Date().toLocaleString());
}
}
2 .在resource文件夹中, 创建spring 的配置文件: spring.xml , 这里特意要注意beans中的命名空间不能少
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
<bean id="xmlTask" class="com.zgd.spring.task.XmlTask"/>
<task:scheduled-tasks>
<!--每5秒钟运行一次-->
<task:scheduled ref="xmlTask" method="task" cron="0/5 * * * * ?"/>
</task:scheduled-tasks>
</beans>
关于cron的语法:
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式:
(1)Seconds Minutes Hours DayofMonth Month DayofWeek Year
(2)Seconds Minutes Hours DayofMonth Month DayofWeek
每一个域可出现的字符如下:
Seconds:可出现", - * /"四个字符,有效范围为0-59的整数
Minutes:可出现", - * /"四个字符,有效范围为0-59的整数
Hours:可出现", - * /"四个字符,有效范围为0-23的整数
DayofMonth:可出现", - * / ? L W C"八个字符,有效范围为1-31的整数
Month:可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc
DayofWeek:可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推
Year:可出现", - * /"四个字符,有效范围为1970-2099年
每一个域都使用数字,但还可以出现如下特殊字符,它们的含义是:
(1)*:表示匹配该域的任意值,假如在Minutes域使用*, 即表示每分钟都会触发事件。
(2)?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和 DayofWeek会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发,实际上并不是这样。
(3)-:表示范围,例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次
(4)/:表示起始时间开始触发,然后每隔固定时间触发一次,例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次.
(5),:表示列出枚举值值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。
(6)L:表示最后,只能出现在DayofWeek和DayofMonth域,如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。
(7)W: 表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一 到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份
(8)LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。
(9)#:用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。
举例如下:
<!-- 每半分钟触发任务 -->
<task:scheduled ref="app" method="execute1" cron="30 * * * * ?"/>
<!-- 每小时的10分30秒触发任务 -->
<task:scheduled ref="app" method="execute2" cron="30 10 * * * ?"/>
<!-- 每天1点10分30秒触发任务 -->
<task:scheduled ref="app" method="execute3" cron="30 10 1 * * ?"/>
<!-- 每月20号的1点10分30秒触发任务 -->
<task:scheduled ref="app" method="execute4" cron="30 10 1 20 * ?"/>
<!-- 每年10月20号的1点10分30秒触发任务 -->
<task:scheduled ref="app" method="execute5" cron="30 10 1 20 10 ?"/>
<!-- 每15秒、30秒、45秒时触发任务 -->
<task:scheduled ref="app" method="execute6" cron="15,30,45 * * * * ?"/>
<!-- 15秒到45秒每隔1秒触发任务 -->
<task:scheduled ref="app" method="execute7" cron="15-45 * * * * ?"/>
<!-- 每分钟的每15秒时任务任务,每隔5秒触发一次 -->
<task:scheduled ref="app" method="execute8" cron="15/5 * * * * ?"/>
<!-- 每分钟的15到30秒之间开始触发,每隔5秒触发一次 -->
<task:scheduled ref="app" method="execute9" cron="15-30/5 * * * * ?"/>
<!-- 每小时的0分0秒开始触发,每隔3分钟触发一次 -->
<task:scheduled ref="app" method="execute10" cron="0 0/3 * * * ?"/>
<!-- 星期一到星期五的10点15分0秒触发任务 -->
<task:scheduled ref="app" method="execute11" cron="0 15 10 ? * MON-FRI"/>
然后我们需要一个测试类来测试:
package com.zgd.spring;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class App{
@Test
public void start(){
System.out.println("启动spring");
while (true){
//使用死循环确保程序持续的运行
}
}
}
运行结果:
2.2 基于@Scheduled注解的方式
这个注解可以传五个参数:
cron:指定cron表达式
zone:官方文档解释:A time zone for which the cron expression will be resolved。指定cron表达式运行的时区
fixedDelay:官方文档解释:An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。
fixedRate:官方文档解释:An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。
initialDelay:官方文档解释:Number of milliseconds to delay before the first execution of a fixedRate() or fixedDelay() task.任务第一次被调用前的延时,单位毫秒
再写一个任务类:
package com.zgd.spring.task;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 注解执行的任务
*/
@Component
public class AnnoTask {
/**
* 使用注解,5秒执行一次
*/
@Scheduled(cron = "0/5 * * * * ?")
public void task(){
System.out.println("注解的方式的任务执行了" + new Date().toLocaleString());
}
}
2 . 在spring配置文件头中添加命名空间及描述,并开启定时任务注解驱动
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="xmlTask" class="com.zgd.spring.task.XmlTask"/>
<!-- <task:scheduled-tasks>
<!–每5秒钟运行一次–>
<task:scheduled ref="xmlTask" method="task" cron="0/5 * * * * ?"/>
</task:scheduled-tasks>-->
<task:annotation-driven />
<context:component-scan base-package="com.zgd.spring.task"/>
</beans>
这里我试了一下,怎么都无法执行定时任务, 后面仔细检查,发现是没有加上注解的自动扫描, 平时开发肯定都会加上这个,但是由于是测试demo所以把这个忘记了.
<context:component-scan base-package="com.zgd.spring.task"/>
运行我们的测试类, 结果如下
---------------------
作者:zzzgd_666
来源:CSDN
原文:https://blog.csdn.net/zzzgd_666/article/details/80723654
版权声明:本文为博主原创文章,转载请附上博文链接!