Spring task定时任务

 

在工做中有用到spring task做为定时任务的处理,spring经过接口TaskExecutorTaskScheduler这两个接口的方式为异步定时任务提供了一种抽象。这就意味着spring允许你使用其余的定时任务框架,固然spring自身也提供了一种定时任务的实现:spring task。spring task支持线程池,能够高效处理许多不一样的定时任务。同时,spring还支持使用Java自带的Timer定时器和Quartz定时框架。限于篇幅,这里将只介绍spring task的使用。java

其实,官方文档已经介绍地足够详细,只不过都是英文版,因此为了更好地理解并使用spring task,首先会对spring task的实现原理作一个简单的介绍,而后经过实际代码演示spring task是如何使用的。这里会涉及到一个很重要的知识点:cron表达式。spring

TaskExecutor和TaskSchedulerapache

TaskExecutor是spring task的第一个抽象,它很天然让人联想到jdk中concurrent包下的Executor,实际上TaskExecutor就是为区别于Executor才引入的,而引入TaskExecutor的目的就是为定时任务的执行提供线程池的支持,那么,问题来了,为何spring不直接使用jdk自带的Executor呢?TaskExecutor源码以下?app

public interface TaskExecutor extends Executor {
    void execute(Runnable var1);
}
  • 1
  • 2
  • 3

那么,答案很显然,TaskExecutor提供的线程池支持也是基于jdk自带的Executor的。用法于Executor没有什么不一样。框架

TaskScheduler是spring task的第二个抽象,那么从字面的意义看,TaskScheduler就是为了提供定时任务的支持咯。TaskScheduler须要传入一个Runnable的任务作为参数,并指定须要周期执行的时间或者触发器,这样Runnable任务就能够周期性执行了。传入时间很好理解,有意思的是传入一个触发器(Trigger)的状况,由于这里须要使用cron表达式去触发一个定时任务,因此有必要先了解下cron表达式的使用。异步

在spring 4.x中已经不支持7个参数的cronin表达式了,要求必须是6个参数(具体哪一个参数后面会说)。cron表达式的格式以下:maven

{秒} {分} {时} {日期(具体哪天)} {月} {星期}
  • 1
  • 秒:必填项,容许的值范围是0-59,支持的特殊符号包括 
    ,
     - * /,表示特定的某一秒才会触发任务,-表示一段时间内会触发任务,*表示每一秒都会触发,/表示从哪个时刻开始,每隔多长时间触发一次任务。
  • 分:必填项,容许的值范围是0-59,支持的特殊符号和秒同样,含义类推
  • 时:必填项,容许的值范围是0-23,支持的特殊符号和秒同样,含义类推
  • 日期:必填项,容许的值范围是1-31,支持的特殊符号相比秒多了?,表示与{星期}互斥,即意味着若明确指定{星期}触发,则表示{日期}无心义,以避免引发冲突和混乱。
  • 月:必填项,容许的值范围是1-12(JAN-DEC),支持的特殊符号与秒同样,含义类推
  • 星期:必填项,容许值范围是1~7 (SUN-SAT),1表明星期天(一星期的第一天),以此类推,7表明星期六,支持的符号相比秒多了?,表达的含义是与{日期}互斥,即意味着若明确指定{日期}触发,则表示{星期}无心义。

好比下面这个cron表达式:测试

// 表达的含义是:每半分钟触发一次任务
30 * * * * ?
  • 1
  • 2

spring提供了一个CronTrigger,经过传入一个Runnable任务和CronTrigger,就可使用cron表达式去指定定时任务了,是否是很是方面。实际上,在工程实践上,cron表达式也是使用不少的。实际上,是执行了下面的代码:ui

scheduler.schedule(task, new CronTrigger("30 * * * * ?"));
  • 1

TaskScheduler抽象的好处是让须要执行定时任务的代码不须要指定特定的定时框架(好比Timer和Quartz)。TaskScheduler的更简单的实现是ThreadPoolTaskScheduler,它实际上代理一个jdk中的SchedulingTaskExecutor,而且也实现了TaskExecutor接口,因此须要常常执行定时任务的场景可使用这个实现(Spring推荐)。咱们再来看一下TaskExecutor和TaskScheduler的类继承关系:spa

spring-task

一般而言,使用spring task实现定时任务有两种方式:注解和xml配置文件。这里使用xml配置文件的方式加以说明。

实战

建立Maven工程,pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.rhwayfun</groupId>
    <artifactId>sring-task-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

开发须要执行定时任务的方法:

package com.rhwayfun.task;

import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author ZhongCB
 * @date 2016年09月10日 14:30
 * @description
 */
@Component
public class App {

    public void execute1(){
        System.out.printf("Task: %s, Current time: %s\n", 1, LocalDateTime.now());
    }

    public void execute2(){
        System.out.printf("Task: %s, Current time: %s\n", 2, LocalDateTime.now());
    }

    public void execute3(){
        System.out.printf("Task: %s, Current time: %s\n", 3, LocalDateTime.now());
    }

    public void execute4(){
        System.out.printf("Task: %s, Current time: %s\n", 4, LocalDateTime.now());
    }

    public void execute5(){
        System.out.printf("Task: %s, Current time: %s\n", 5, LocalDateTime.now());
    }

    public void execute6(){
        System.out.printf("Task: %s, Current time: %s\n", 6, LocalDateTime.now());
    }

    public void execute7(){
        System.out.printf("Task: %s, Current time: %s\n", 7, LocalDateTime.now());
    }

    public void execute8(){
        System.out.printf("Task: %s, Current time: %s\n", 8, LocalDateTime.now());
    }

    public void execute9(){
        System.out.printf("Task: %s, Current time: %s\n", 9, LocalDateTime.now());
    }

    public void execute10(){
        System.out.printf("Task: %s, Current time: %s\n", 10, LocalDateTime.now());
    }

    public void execute11(){
        System.out.printf("Task: %s, Current time: %s\n", 11, LocalDateTime.now());
    }

}

spring配置文件以下:

<?xml version="1.0" encoding="UTF-8"?>
<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 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">

    <!-- 配置注解扫描 -->
    <context:component-scan base-package="com.rhwayfun.task"/>

    <task:scheduler id="taskScheduler" pool-size="100" />

    <task:scheduled-tasks scheduler="taskScheduler">
        <!-- 每半分钟触发任务 -->
        <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"/>
    </task:scheduled-tasks>

</beans>

编写测试代码:

package com.rhwayfun.task;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author ZhongCB
 * @date 2016年09月10日 14:55
 * @description
 */
public class AppTest {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/app-context-task.xml");
    }
}

运行测试代码,控制台会定时输出每一个定时任务的日志信息,说明测试经过。

 

1.cron表达式格式:

{秒} {分} {时} {日} {月} {周} {年(可选)}

2.cron各选项的取值范围及解释:
{秒}:取值范围(0-59),不容许为空值,若值不合法,调度器将抛出SchedulerException异常
{分}:取值范围(0-59),不容许为空值,若值不合法,调度器将抛出SchedulerException异常
{时}:取值范围(0-23),不容许为空值,若值不合法,调度器将抛出SchedulerException异常

"*" 表明每隔1分/秒/时触发;
"," 表明在指定的分/秒/时触发,好比"10,20,40"表明10分/秒/时、20分/秒/时和40分/秒/时时触发任务
"-" 表明在指定的范围内触发,好比"5-30"表明从5分/秒/时开始触发到30分/秒/时结束触 发,每隔1分/秒/时触发
"/" 表明触发步进(step),"/"前面的值表明初始值("*"等同"0"),后面的值表明偏移量,好比"0/25"或者"*/25"表明从0分/秒/时开始,每隔25分/秒/时触发1次,即0分/秒/时触发1次,第25分/秒/时触发1次,第50分/秒/时触发1次;"5/25"表明5分/秒/时触发1次,30分/秒/时触发1次,55分/秒/时触发1次;"10-45/20"表明在[10,45]内步进20分/秒/时命中的时间点触发,即10分/秒/时触发1次,30分/秒/时触发1次

{日}:取值范围(1-31),不容许为空值,若值不合法,调度器将抛出SchedulerException异常

"*" 表明天天触发;
"?" 与{周}互斥,即意味着若明确指定{周}触发,则表示{日}无心义,以避免引发 冲突和混乱;
"," 表明在指定的日期触发,好比"1,10,20"表明1号、10号和20号这3天触发;
"-" 表明在指定的日期范围内触发,好比"10-15"表明从10号开始触发到15号结束触发,每隔1天触发
"/" 表明触发步进(step),"/"前面的值表明初始值("*"等同"1"),后面的值表明偏移量,好比"1/5"或者"*/5"表明从1号开始触发,每隔5天触发1次;"10/5"表明从10号开始触发,之后每隔5天触发一次;"1-10/2"表达式意味着在[1,10]范围内,每隔2天触发,即1号,3号,5号,7号,9号触发
"L" 若是{日}占位符若是是"L",即意味着当月的最后一天触发
"W "意味着在本月内离当天最近的工做日触发,所谓最近工做日,即当天到工做日的先后最短距离,若是当天即为工做日,则距离为0;所谓本月内的说法,就是不能跨月取到最近工做日,即便前/后月份的最后一天/第一天确实知足最近工做日;所以,"LW"则意味着本月的最后一个工做日触发,"W"强烈依赖{月}
"C" 根据日历触发,因为使用较少,暂时不作解释

{月}:取值范围(1-12或JAN-DEC),不容许为空值,若值不合法,调度器将抛出SchedulerException异常
{周}:取值范围(1-7或SUN-SAT),1表示星期天,2表示星期一, 依次类推,不容许为空值,若值不合法,调度器将抛出SchedulerException异常

"*" 表明每星期都触发;
"?" 与{日}互斥,即意味着若明确指定{日}触发,则表示{周}无心义,以避免引发冲突和混乱
"," 表明在指定的星期约定触发,好比"1,3,5"表明星期天、星期二和星期四触发
"-" 表明在指定的星期范围内触发,好比"2-4"表明从星期一开始触发到星期三结束触发,每隔1天触发
"/" 表明触发步进(step),"/"前面的值表明初始值("*"等同"1"),后面的值表明偏移量,好比"1/3"或者"*/3"表明从星期天开始触发,每隔3天触发1次;"1-5/2"表达式意味着在[1,5]范围内,每隔2天触发,即星期天、星期2、星期四触发
"L" 若是{周}占位符若是是"L",即意味着星期的的最后一天触发,即星期六触发,L= 7或者 L = SAT,所以,"5L"意味着一个月的最后一个星期四触发
"#" 用来指定具体的周数,"#"前面表明星期,"#"后面表明本月第几周,好比"2#2"表示本月第二周的星期一,"5#3"表示本月第三周的星期四,所以,"5L"这种形式只不过是"#"的特殊形式而已
"C" 根据日历触发,因为使用较少,暂时不作解释

{年}:取值范围(1970-2099),容许为空值,(非空时)若值不合法,调度器将抛出SchedulerException异常

注:
①.秒、分、时、月、年这5项的占位符以及占位符的使用规则是一致的(只是单位上的区别),日、周中标红的两条须要重点比较!

②.“?”和“L”字符仅被用于{日}和{周}两个子表达式,表示不指定值 。对于“?”的应用,当2个子表达式其中之一被指定了值之后,为了不冲突,须要将另外一个子表达式的值设为“?”;在{日}表达式中,“L”表示一个月的最后一天,在{周}自表达式中,“L”表示一个星期的最后一天。若是在“L”前有具体的内容,它就具备其余的含义了。例如:“6L”表示这个月的倒数第6天,“FRIL”表示这个月的最一个星期五 。

③.在使用“L”参数时,不要指定列表或范围,由于这会致使问题

经常使用案例:

"0 0 10,14,16 * * ?" 天天上午10点,下午2点,4点 
"0 0/30 9-17 * * ?" 朝九晚五工做时间内每半小时 
"0 0 12 ? * WED" 表示每一个星期三中午12点 
"0 0 12 * * ?" 天天中午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期间的每1分钟触发 
"0 0/5 14 * * ?" 在天天下午2点到下午2:55期间的每5分钟触发 
"0 0/5 14,18 * * ?" 在天天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 
"0 0-5 14 * * ?" 在天天下午2点到下午2:05期间的每1分钟触发 
"0 10,44 14 ? 3 WED" 每一年三月的星期三的下午2:10和2: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触发 
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每个月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6#3" 每个月的第三个星期五上午10:15触发
"30 * * * * ?" 每半分钟触发任务
"30 10 * * * ?" 每小时的10分30秒触发任务
"30 10 1 * * ?" 天天1点10分30秒触发任务
"30 10 1 20 * ?" 每个月20号1点10分30秒触发任务
"30 10 1 20 10 ? *" 每一年10月20号1点10分30秒触发任务
"30 10 1 20 10 ? 2011" 2011年10月20号1点10分30秒触发任务
"30 10 1 ? 10 * 2011" 2011年10月天天1点10分30秒触发任务
"30 10 1 ? 10 SUN 2011" 2011年10月每周日1点10分30秒触发任务
"15,30,45 * * * * ?" 每15秒,30秒,45秒时触发任务
"15-45 * * * * ?" 15到45秒内,每秒都触发任务
"15/5 * * * * ?" 每分钟的每15秒开始触发,每隔5秒触发一次
"15-30/5 * * * * ?" 每分钟的15秒到30秒之间开始触发,每隔5秒触发一次
"0 0/3 * * * ?" 每小时的第0分0秒开始,每三分钟触发一次
"0 15 10 ? * MON-FRI" 星期一到星期五的10点15分0秒触发任务
"0 15 10 L * ?" 每月最后一天的10点15分0秒触发任务
"0 15 10 LW * ?" 每月最后一个工做日的10点15分0秒触发任务
"0 15 10 ? * 5L" 每月最后一个星期四的10点15分0秒触发任务
"0 15 10 ? * 5#3" 每月第三周的星期四的10点15分0秒触发任务
"0 0 0 * * ?" 天天晚上12点触发任务
相关文章
相关标签/搜索