spring task定时器的配置使用 1.配置文件配置 2.注解配置 3.配置多线程定时任务 4.cron表达式

spring task的配置方式有两种:配置文件配置和注解配置。

在applicationContext.xml中增加spring task的命名空间:

xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="            
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-4.1.xsd">

 在applicationContext.xml中配置spring task:

    <!-- 配置文件方式配置spring task -->
    <task:scheduled-tasks scheduler="scheduler">
        <task:scheduled ref="taskJob" method="test2"
            cron="*/5 * * * * ?" />        
    </task:scheduled-tasks>  

 编写定时任务方法:

// spring task配置文件方式
    public void test2() {
        String time = DateFormat.getDateTimeInstance().format(new Date());
        System.out.println("test2定时器触发打印" + time);
    }

2.注解配置

在applicationContext.xml中增加spring task的命名空间:

xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="            
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-4.1.xsd">

 在applicationContext.xml中配置spring task:

    <!-- 注解方式配置spring task -->
    <task:annotation-driven scheduler="scheduler" executor="executor"/>

如果使用springboot,则无需配置applicationContext.xml,直接添加@EnableScheduling注解即可。

 @EnableScheduling注解的作用是发现注解@Scheduled的任务并后台执行。

@SpringBootApplication
@EnableScheduling //允许支持schedule定时任务
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 编写定时任务方法:

// spring task注解方式
    @Scheduled(cron = "*/5 * * * * ?")
    public void test1() {
        String time = DateFormat.getDateTimeInstance().format(new Date());
        System.out.println("test1定时器触发打印" + time);
    }

3.配置多线程定时任务

上述方法可以实现定时任务,方式也比较简单,不用配置什么文件啥的,但你会发现一个问题,就是不论定时任务被安排在多少个class类中,其依然是单线程执行定时任务(串行任务):

2016-02-14-15-05 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTasks.executeUploadTask 定时任务1:15,name:pool-2-thread-1
     定时任务2:15
2016-02-14-15-06 [pool-2-thread-1] [com.autonavi.task.ScheduledTasks] [INFO] - ScheduledTest.executeUploadTask 定时任务2:15,name:pool-2-thread-1

上述执行结果中ScheduledTest和ScheduledTasks是两个独立类,都有各自定时任务,但运行时起Thread Name都是一样的pool-2-thread-1,因此每个定时任务若要新启一个线程,需要自行编写实现或者配置文件。

SpringBoot定时任务默认单线程,多线程需要在配置文件applicationContext.xml中添加如下内容:

<!-- 注解方式配置spring task -->
<task:annotation-driven scheduler="scheduler" executor="executor"/>
        
<!-- 调度线程池的大小 -->
<task:scheduler id="scheduler" pool-size="10"/>
<task:executor id="executor" pool-size="10" />  

效果如下,每个调度处理一个任务,每个调度也是一个子线程: 
spring task定时器的配置使用
1.配置文件配置
2.注解配置
3.配置多线程定时任务
4.cron表达式

4.cron表达式

在spring 4.x中已经不支持7个参数的cronin表达式了,要求必须是6个参数,cron表达式的格式如下。

Seconds Minutes Hours DayofMonth Month DayofWeek

Seconds:可出现", - * /"四个字符,有效范围为0-59的整数 
Minutes:可出现", - * /"四个字符,有效范围为0-59的整数 
Hours:可出现", - * /"四个字符,有效范围为0-23的整数 
DayofMonth:可出现", - * / ? L W C"八个字符,有效范围为0-31的整数 
Month:可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEc 
DayofWeek:可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推 

每一个域都使用数字,但还可以出现如下特殊字符,它们的含义是: 
(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,表示某月的第二个星期三。

举几个例子: 

0 0 2 1 * ? * 表示在每月的1日的凌晨2点调度任务 
0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业 
0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作

Cron表达式范例:

                 每隔5秒执行一次:*/5 * * * * ?

                 每隔1分钟执行一次:0 */1 * * * ?

                 每天23点执行一次:0 0 23 * * ?

                 每天凌晨1点执行一次:0 0 1 * * ?

                 每月1号凌晨1点执行一次:0 0 1 1 * ?

                 每月最后一天23点执行一次:0 0 23 L * ?

                 每周星期天凌晨1点实行一次:0 0 1 ? * L

                 在26分、29分、33分执行一次:0 26,29,33 * * * ?

                 每天的0点、13点、18点、21点都执行一次:0 0 0,13,18,21 * * ?

参考:

cron表达式详解

SpringBoot定时任务说明