org.springframework.scheduling.timer
Class TimerTaskExecutor

java.lang.Object
  extended by org.springframework.scheduling.timer.TimerTaskExecutor
All Implemented Interfaces:
java.util.concurrent.Executor, BeanNameAware, DisposableBean, InitializingBean, AsyncTaskExecutor, TaskExecutor, SchedulingTaskExecutor

public class TimerTaskExecutor
extends java.lang.Object
implements SchedulingTaskExecutor, BeanNameAware, InitializingBean, DisposableBean

TaskExecutor implementation that uses a single Timer for executing all tasks, effectively resulting in serialized asynchronous execution on a single thread.

Since:
2.0
Author:
Juergen Hoeller
See Also:
Timer

Field Summary
protected  org.apache.commons.logging.Log logger
           
 
Fields inherited from interface org.springframework.core.task.AsyncTaskExecutor
TIMEOUT_IMMEDIATE, TIMEOUT_INDEFINITE
 
Constructor Summary
TimerTaskExecutor()
          Create a new TimerTaskExecutor that needs to be further configured and initialized.
TimerTaskExecutor(java.util.Timer timer)
          Create a new TimerTaskExecutor for the given Timer.
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
protected  java.util.Timer createTimer()
          Create a new Timer instance.
 void destroy()
          Cancel the Timer on bean factory shutdown, stopping all scheduled tasks.
 void execute(java.lang.Runnable task)
          Schedules the given Runnable on this executor's Timer instance, wrapping it in a DelegatingTimerTask.
 void execute(java.lang.Runnable task, long startTimeout)
          Execute the given task.
protected  java.util.Timer getTimer()
          Return the underlying Timer behind this TimerTaskExecutor.
 boolean prefersShortLivedTasks()
          This task executor prefers short-lived work units.
 void setBeanName(java.lang.String beanName)
          Set the name of the bean in the bean factory that created this bean.
 void setDelay(long delay)
          Set the delay to use for scheduling tasks passed into the plain execute(Runnable) method.
 void setTimer(java.util.Timer timer)
          Set the Timer to use for this TimerTaskExecutor, for example a shared Timer instance defined by a TimerFactoryBean.
<T> java.util.concurrent.Future<T>
submit(java.util.concurrent.Callable<T> task)
          Submit a Callable task for execution, receiving a Future representing that task.
 java.util.concurrent.Future<?> submit(java.lang.Runnable task)
          Submit a Runnable task for execution, receiving a Future representing that task.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final org.apache.commons.logging.Log logger
Constructor Detail

TimerTaskExecutor

public TimerTaskExecutor()
Create a new TimerTaskExecutor that needs to be further configured and initialized.

See Also:
setTimer(java.util.Timer), afterPropertiesSet()

TimerTaskExecutor

public TimerTaskExecutor(java.util.Timer timer)
Create a new TimerTaskExecutor for the given Timer.

Parameters:
timer - the Timer to wrap
Method Detail

setTimer

public void setTimer(java.util.Timer timer)
Set the Timer to use for this TimerTaskExecutor, for example a shared Timer instance defined by a TimerFactoryBean.

If not specified, a default internal Timer instance will be used.

Parameters:
timer - the Timer to use for this TimerTaskExecutor
See Also:
TimerFactoryBean

setDelay

public void setDelay(long delay)
Set the delay to use for scheduling tasks passed into the plain execute(Runnable) method. Default is 0.

Note that calls to execute(Runnable, long) will use the given timeout as delay if it is lower than the general delay.

Parameters:
delay - the delay in milliseconds before the task is to be executed

setBeanName

public void setBeanName(java.lang.String beanName)
Description copied from interface: BeanNameAware
Set the name of the bean in the bean factory that created this bean.

Invoked after population of normal bean properties but before an init callback such as InitializingBean.afterPropertiesSet() or a custom init-method.

Specified by:
setBeanName in interface BeanNameAware
Parameters:
beanName - the name of the bean in the factory. Note that this name is the actual bean name used in the factory, which may differ from the originally specified name: in particular for inner bean names, the actual bean name might have been made unique through appending "#..." suffixes. Use the BeanFactoryUtils.originalBeanName(String) method to extract the original bean name (without suffix), if desired.

afterPropertiesSet

public void afterPropertiesSet()
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean

createTimer

protected java.util.Timer createTimer()
Create a new Timer instance. Called by afterPropertiesSet if no Timer has been specified explicitly.

The default implementation creates a plain non-daemon Timer. If overridden, subclasses must take care to ensure that a non-null Timer is returned from the execution of this method.

See Also:
afterPropertiesSet(), Timer.Timer(String, boolean)

getTimer

protected final java.util.Timer getTimer()
Return the underlying Timer behind this TimerTaskExecutor.


execute

public void execute(java.lang.Runnable task)
Schedules the given Runnable on this executor's Timer instance, wrapping it in a DelegatingTimerTask.

Specified by:
execute in interface java.util.concurrent.Executor
Specified by:
execute in interface TaskExecutor
Parameters:
task - the task to be executed

execute

public void execute(java.lang.Runnable task,
                    long startTimeout)
Description copied from interface: AsyncTaskExecutor
Execute the given task.

Specified by:
execute in interface AsyncTaskExecutor
Parameters:
task - the Runnable to execute (never null)
startTimeout - the time duration (milliseconds) within which the task is supposed to start. This is intended as a hint to the executor, allowing for preferred handling of immediate tasks. Typical values are AsyncTaskExecutor.TIMEOUT_IMMEDIATE or AsyncTaskExecutor.TIMEOUT_INDEFINITE (the default as used by TaskExecutor.execute(Runnable)).

submit

public java.util.concurrent.Future<?> submit(java.lang.Runnable task)
Description copied from interface: AsyncTaskExecutor
Submit a Runnable task for execution, receiving a Future representing that task. The Future will return a null result upon completion.

Specified by:
submit in interface AsyncTaskExecutor
Parameters:
task - the Runnable to execute (never null)
Returns:
a Future representing pending completion of the task

submit

public <T> java.util.concurrent.Future<T> submit(java.util.concurrent.Callable<T> task)
Description copied from interface: AsyncTaskExecutor
Submit a Callable task for execution, receiving a Future representing that task. The Future will return the Callable's result upon completion.

Specified by:
submit in interface AsyncTaskExecutor
Parameters:
task - the Callable to execute (never null)
Returns:
a Future representing pending completion of the task

prefersShortLivedTasks

public boolean prefersShortLivedTasks()
This task executor prefers short-lived work units.

Specified by:
prefersShortLivedTasks in interface SchedulingTaskExecutor
Returns:
true if this TaskExecutor prefers short-lived tasks

destroy

public void destroy()
Cancel the Timer on bean factory shutdown, stopping all scheduled tasks.

Specified by:
destroy in interface DisposableBean
See Also:
Timer.cancel()