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
lua:
local access_token = ngx.var.cookie_SGAccessToken
if access_token then
ngx.header["Set-Cookie"] = "SGAccessToken="..access_token.."; path=/;Max-Age=3000"
end