spring task入门

 参考文档:http://blog.csdn.net/u011116672/article/details/52517247
   https://www.cnblogs.com/hongwz/p/5642497.html

 spring task为Spring3.0后自主开发的轻量级定时任务工具,使用方便不需要引入spring之外的其他jar包,并且支持线程池,可以高效的处理很多不同的定时任务。
首先说明spring通过TaskExecutorTaskScheduler这两个接口的方式为异步定时任务提供了一种抽象。这就意味着spring支持使用其他的定时任务框架比如quartz。本文将主要介绍spring task注解和配置文件两种方式的使用。
 基础部分:

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

public interface TaskExecutor extends Executor {
    void execute(Runnable var1);
}
显而易见TaskExecutor提供的线程池支持也是基于jdk自带的Executor,用法相同

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

corn表达式:

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

 /**   1  {秒}  {分} {时}  {日}  {月} {星期}
    秒:必填项,允许的值范围是0-59,支持的特殊符号包括
    , - * /,,表示特定的某一秒才会触发任务,-表示一段时间内会触发任务,*表示每一秒都会触发,
    /表示从哪一个时刻开始,每隔多长时间触发一次任务。
    分:必填项,允许的值范围是0-59,支持的特殊符号和秒一样,含义类推
    时:必填项,允许的值范围是0-23,支持的特殊符号和秒一样,含义类推
    日期:必填项,允许的值范围是1-31,支持的特殊符号相比秒多了?,表示与{星期}互斥,即意味着若明确指定{星期}触发,则表示{日期}无意义,以免引起冲突和混乱。
    月:必填项,允许的值范围是1-12(JAN-DEC),支持的特殊符号与秒一样,含义类推
    星期:必填项,允许值范围是1~7 (SUN-SAT),1代表星期天(一星期的第一天),以此类推,7代表星期六,支持的符号相比秒多了?,表达的含义是与{日期}互斥,即意味着若明确指定{日期}触发,则表示{星期}无意义。
    */
spring提供了一个 CronTrigger ,通过传入一个Runnable任务和CronTrigger,就可以使用cron表达式去指定定时任务了,是不是非常方面。实际上,在工程实践上,cron表达式也是使用很多的。实际上,是执行了下面的代码:
scheduler.schedule(task, new CronTrigger("30 * * * * ?"));
TaskScheduler抽象的好处是让需要执行定时任务的代码不需要指定特定的定时框架(比如Timer和Quartz)。 TaskScheduler的更简单的实现是 ThreadPoolTaskScheduler ,它实际上代理一个jdk中的 SchedulingTaskExecutor 并且也实现了TaskExecutor接口,所以需要经常执行定时任务的场景可以使用这个实现(Spring推荐)。 我们再来看一下TaskExecutor和TaskScheduler的类继承关系

实战:

第一种:配置文件实现

第一步,编写task job类

package com.hfpmp.test.controller;

import org.springframework.stereotype.Component;

/**
 * @author: noob
 * @description :
 * @Date : 10:16 2018/01/26
 */
@Component("task")
public class Task {
    public void show(){
        System.out.println("任务正在进行。。。");
    }
}


 第二步: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.hfpmp.test.controller"/>
    <task:scheduler id="taskScheduler" pool-size="100" />
    <task:scheduled-tasks scheduler="taskScheduler">
        <!-- 每半分钟触发任务 -->
        <task:scheduled ref="task" method="show" cron="/30 * * * * ?"/>
    </task:scheduled-tasks>
</beans>  
说明:ref参数指定的即任务类,method指定的即需要运行的方法,cron及cronExpression表达式。
第二种:使用注解的方式@Scheduled

第一步:编写pojo

package com.hfpmp.test.controller;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author: noob
 * @description :
 * @Date : 10:16 2018/01/26
 */
@Component("task")
public class Task {
    @Scheduled(cron = "30 * * * * ?")
    public void show(){
        System.out.println("任务正在进行。。。");
    }
}
第二步:编写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.hfpmp.test.controller"/>
    <!--开启这个配置,spring才能识别@Scheduled注解-->
    <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
    <task:scheduler id="qbScheduler" pool-size="10"/>
</beans>
说明:理论上只需要加上<task:annotation-driven />这句配置就可以了,这些参数都不是必须的。
知识点:





你好

猜你喜欢

转载自blog.csdn.net/tomatofireegg/article/details/79169783