The Spring Framework provides abstractions for asynchronous execution and scheduling of tasks with the 'TaskExecutor' and 'TaskScheduler' ibsp; Spring also Provided Implementations of these Interfaces which supports 'Thread Pools' or delegation to CommonJ within an application server environment.
Spring also features integration classes for supporting scheduling with the Timer, part of the JDK since1.3, and the Quartz Scheduler.
Both of those schedulers are set up using a FactoryBean with optional references to Timer or Trigger instances, respectively. And, a convenience class for both the Quartz Scheduler and the Timer is available that allows you to invoke a method of an existing target object (analogous to the normal MethodInvokingFactoryBean operation).
Spring's TaskExecutor interface has a single method execute(Runnable task) that accepts a task for execution based on the semantics and configuration of the thread pool.
The TaskExecutor was originally created to give other Spring components an abstraction for thread pooling where needed. Components such as the ApplicationEventMulticaster, JMS's AbstractMessageListenerContainer, and Quartz integration all use the TaskExecutor abstraction to pool threads. However, if your beans need thread pooling behavior, it is possible to use this abstraction for your own needs.
TaskExecutor types:
There are a number of pre-built implementations of TaskExecutor
SimpleAsyncTaskExecutor
Spring also features integration classes for supporting scheduling with the Timer, part of the JDK since1.3, and the Quartz Scheduler.
Both of those schedulers are set up using a FactoryBean with optional references to Timer or Trigger instances, respectively. And, a convenience class for both the Quartz Scheduler and the Timer is available that allows you to invoke a method of an existing target object (analogous to the normal MethodInvokingFactoryBean operation).
Spring's TaskExecutor interface has a single method execute(Runnable task) that accepts a task for execution based on the semantics and configuration of the thread pool.
The TaskExecutor was originally created to give other Spring components an abstraction for thread pooling where needed. Components such as the ApplicationEventMulticaster, JMS's AbstractMessageListenerContainer, and Quartz integration all use the TaskExecutor abstraction to pool threads. However, if your beans need thread pooling behavior, it is possible to use this abstraction for your own needs.
TaskExecutor types:
There are a number of pre-built implementations of TaskExecutor
SimpleAsyncTaskExecutor
SyncTaskExecutor
ConcurrentTaskExecutor
and etc.
Using a TaskExecutor:
Spring's TaskExecutor implementations are used as simple JavaBeans
as Following
import org.springframework.core.task.TaskExecutor;
public class TaskExecutorExample {
private class MessagePrinterTask implements Runnable {
private String message;
public MessagePrinterTask(String message) {
this.message = message;
}
public void run() {
System.out.println(message);
}
}
private TaskExecutor taskExecutor;
public TaskExecutorExample(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void printMessages() {
for(int i = 0; i < 25; i++) {
taskExecutor.execute(new MessagePrinterTask("Message" + i));
}
}
}
As you can see, rather than retrieving a thread from the pool and executing yourself, you add your Runnable to the queue and the TaskExecutor uses its internal rules to decide when the task gets
executed.
To configure the rules that the TaskExecutor will use, simple bean properties have been exposed.
<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="5" />
<property name="maxPoolSize" value="10" />
<property name="queueCapacity" value="25" />
</bean>
<bean id="taskExecutorExample" class="TaskExecutorExample">
<constructor-arg ref="taskExecutor" />
</bean>
Task Scheduler:
In addition to the 'TaskExecutor' abstraction, Spring 3.0 introduces a TaskScheduler with a variety of methods for scheduling tasks to run at some point in the future.
In addition to the 'TaskExecutor' abstraction, Spring 3.0 introduces a TaskScheduler with a variety of methods for scheduling tasks to run at some point in the future.
Methods in TaskScheduler Interface
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);
The simplest method is the one named 'schedule' that takes a Runnable and Date only. That will cause the task to run once after the specified time. All of the other methods are capable of scheduling tasks to run repeatedly. The fixed-rate and fixed-delay methods are for simple, periodic execution, but the method that accepts a Trigger is much more flexible.
Trigger:
public interface Trigger {
Date nextExecutionTime(TriggerContext triggerContext);
}
The 'TriggerContex' is the most important part. It encapsulates all of the relevant data.The 'TriggerContext' is an interface (a 'SimpleTriggerContext' implementation is used by default). The methods are available for Trigger implementations.
public interface TriggerContext {
Date lastScheduledExecutionTime();
Date lastActualExecutionTime();
Date lastCompletionTime();
}
Trigger implementations:
Spring provides two implementations of the Trigger interface. The most interesting one is the
CronTrigger. It enables the scheduling of tasks based on cron expressions. For example the following
task is being scheduled to run 15 minutes past each hour but only during the 9-to-5 "business hours"
on weekdays.
scheduler.schedule(task, new CronTrigger("* 15 9-17 * * MON-FRI"));
And Another Implementations is
implementation is a 'PeriodicTrigger' that 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.
0 comments:
Post a Comment