Java Reference
In-Depth Information
<bean
id="timerTaskExecutorWithoutScheduledTimerTasks"
class="org.springframework.scheduling.timer.TimerTaskExecutor"
p:delay="10000" />
<bean
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
p:corePoolSize="50"
p:daemon="false"
p:waitForTasksToCompleteOnShutdown="true"
p:maxPoolSize="100"
p:allowCoreThreadTimeOut="true" />
<!-- client bean -->
<bean
id="springExecutorsDemo"
class="com.apress.springenterpriserecipes.spring3.
executors.SpringExecutorsDemo" />
</beans>
The previous code shows different implementations of the TaskExecutor interface. The first bean,
the TaskExecutorAdapter instance, is a simple wrapper around a java.util.concurrence.Executor
instance so that you can deal with in terms of the Spring TaskExecutor interface. This is only slightly
useful because you could conceptually deal in terms of the Executor interface now because Spring 3.0
updates the TaskExecutor interface to extend Executor . You use Spring here to configure an instance of
an Executor and pass it in as the constructor argument.
SimpleAsyncTaskExecutor provides a new Thread for each job submitted. It does no thread pooling
or reuse. Each job submitted runs asynchronously in a thread.
SyncTaskExecutor is the simplest of the implementations of TaskExecutor . Submission of a job is
synchronous and tantamount to launching a Thread , running it, and then join() ing it immediately. It's
effectively the same as manually invoking the run() method in the calling thread, skipping threading all
together.
TimerTaskExecutor uses a java.util.Timer instance and manages jobs
( java.util.concurrent.Callable<T> or java.lang.Runnable instances) for you by running them
on the Timer . You can specify a delay when creating the TimerTaskExecutor , after which all submitted
jobs will start running. Internally, the TimerTaskExecutor converts Callable<T> instances or Runnable
instances that are submitted into TimerTasks , which it then schedules on the Timer . If you schedule
multiple jobs, they will be run serialized on the same thread with the same Timer . If you don't
specify a Timer explicitly, a default one will be created. If you want to explicitly register TimerTask s
on the Timer , use the org.springframework.scheduling.timer.TimerFactoryBean 's scheduledTimerTasks
property. The TimerTaskExecutor doesn't surface methods for more advanced scheduling like the
Timer class does. If you want to schedule at fixed intervals, at a certain Date (point in time) or
for a certain period, you need to manipulate the TimerTask itself. You can do this with the
org.springframework.scheduling.timer.ScheduledTimerTask class, which provides a readily configured
TimerTask that the TimerFactoryBean will schedule appropriately.
To submit jobs just as you have with other TaskExecutors , after a delay simply configure a
TimerFactoryBean and then submit as usual:
<bean id ="timerTaskExecutorWithoutScheduledTimerTasks"
class="org.springframework.scheduling.timer.TimerTaskExecutor" p:delay="10000" />
Search WWH ::




Custom Search