关于Spring定时任务的说明

    现在使用Spring来做定时任务是非常简单的一件事,只需要像 这里写的,加两个注解就可以实现定时任务的功能。但Spring对这块的抽象是怎么做的呢?下面来讲解一下。
对于定时任务,Spring做了抽象,并形成了下面的这几个接口。
  • TaskExecutor
  • TaskScheduler
  • Trigger
  • TriggerContext

TaskExecutor:
    这个接口用来做和Executors相关的一些处理。Executors是从Java5时引入的线程池(thread pools)的概念。这个接口的主要目的是抽象化使用线程池的使用方式,但Spring不担保底层的实现是以线程池的方法实现的。
    这个接口只有一个方法:void execute(Runnable task)。这个接口的实现类有很多,在能够执行任务的基础上,增加了很多实用的功能,例如使用线程池,配置线程池,异步任务,对任务进行设置定时启动等等。具体的实现类如下:
  • SimpleAsyncTaskExecutor: Does not reuse threads. It starts a new thread and is asynchronus.
  • SyncTaskExecutor: No thread reuse and is synchonous. Instead, each invocation takes place in the calling thread.
  • ConcurrentTaskExecutor: Exposes the Java 5 java.util.concurrent.Executor.
  • SimpleThreadPoolTaskExecutor: It is a subclass of Quartz’s SimpleThreadPool which listens to Spring’s lifecycle callbacks.
  • ThreadPoolTaskExecutor: It exposes bean properties for configuring a ThreadPoolExecutor configuration and and wraps it in a TaskExecutor.
  • TimerTaskExecutor: This implementation uses a single TimerTask as its backing implementation. It’s different from the SyncTaskExecutor in that the method invocations are executed in a separate thread, although they are synchronous in that thread.
  • WorkManagerTaskExecutor: This implementation uses the CommonJ WorkManager as its backing implementation

TaskScheduler:
    这个接口是从Spring3.0才有的。使用这个接口就比使用TaskExecutor接口相对简单一些,因为它的提供了很多关于定时启动的方法。具体的接口定义如下:
    public interface TaskScheduler {
        ScheduledFuture schedule(Runnable task, Trigger trigger);
        ScheduledFuture schedule(Runnable task, Date startTime);
        ScheduledFuture scheduleAtFixedRate(Runnable task, Date startTime, long period);
        ScheduledFuture scheduleAtFixedRate(Runnable task, long period);
        ScheduledFuture scheduleWithFixedDelay(Runnable task, Date startTime, long delay);
        ScheduledFuture scheduleWithFixedDelay(Runnable task, long delay);
    }
具体的实现类如下:
  • TimerManagerTaskScheduler: Delegates to a CommonJ TimerManager instance, typically configured with a JNDI-lookup.
  • ThreadPoolTaskScheduler: Used whenever external thread management is not a requirement. Internally, it delegates to a ScheduledExecutorService instance.

Trigger:
    这个接口出现的主要想法是:有一些任务的执行,是由过去的执行结果或任意条件来决定的,为了支持这种需求,产生了Trigger。通过TriggerContext,我们可以取得一些以前的执行信息。上面的TaskScheduler中的schedule方法的一个参数类型就是这个接口。
这个接口的定义非常简单:
    public interface Trigger {
        Date nextExecutionTime(TriggerContext triggerContext);
    }
Spring提供了两个实现类:
    CronTrigger: It enables the scheduling of tasks based on cron expressions.
    PeriodicTrigger: It accepts a fixed period, an optional initial delay value, and a boolean to indicate whether the period should be interpreted as a fixed-rate or a fixed-delay.
下面是一个使用例子:
    ThreadPoolTaskScheduler scheduler = (ThreadPoolTaskScheduler) appContext.getBean("scheduler");
    CronTrigger trigger = new CronTrigger("*/5 * * * * MON-FRI");
    ScheduledFuture scedulefuture= scheduler.schedule(taskObject, trigger );
    

TriggerContext:
    上面提到了,这个接口提供了一些以前执行任务的信息,具体的方法如下:
  • lastActualExecutionTime()
  • lastCompletionTime()
  • lastScheduledExecutionTime()
从名字上就可以看出这些方法的意思。
它也有一个实现类:
    SimpleTriggerContext
这个实现类在实现接口方法的基础上,还增加了一个方法:update(Date lastScheduledExecutionTime, Date lastActualExecutionTime, Date lastCompletionTime)。这个方法是来更新lastScheduledExecutionTime,lastActualExecutionTime,lastCompletionTime这三个接口返回值的。
    
    
参考:
http://javabeat.net/task-execution-scheduling-spring/
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-scheduled

你可能感兴趣的:(spring)