The Spring Framework

org.springframework.scheduling.timer
Class TimerTaskExecutor

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

public class TimerTaskExecutor
extends Object
implements SchedulingTaskExecutor, 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  Log logger
          The shared Log instance.
 
Constructor Summary
TimerTaskExecutor()
          Create a new TimerTaskExecutor that needs to be further configured and initialized.
TimerTaskExecutor(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  Timer createTimer()
          Create a new Timer instance.
 void destroy()
          Cancel the Timer on bean factory shutdown, stopping all scheduled tasks.
 void execute(Runnable task)
          Schedules the given Runnable on this executor's Timer instance, wrapping it in a DelegatingTimerTask.
 boolean prefersShortLivedTasks()
          This task executor prefers short-lived work units.
 void setDelay(int delay)
          Set the delay to use for scheduling tasks passed into the execute method.
 void setTimer(Timer timer)
          Set the Timer to use for this TimerTaskExecutor, for example a shared Timer instance defined by a TimerFactoryBean.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final Log logger
The shared Log instance.

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(Timer timer)
Create a new TimerTaskExecutor for the given Timer.

Parameters:
timer - the Timer to wrap
Throws:
IllegalArgumentException - if the supplied Timer is null
Method Detail

setTimer

public void setTimer(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 Timer instance will be used.

Parameters:
timer - the Timer to use for this TimerTaskExecutor (may be null)
See Also:
TimerFactoryBean

setDelay

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

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

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 Timer createTimer()
Create a new Timer instance. Called by afterPropertiesSet if no Timer has been specified explicitly.

Default implementation creates a plain daemon Timer.

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

Returns:
a new Timer instance
See Also:
afterPropertiesSet(), Timer.Timer(boolean)

execute

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

Specified by:
execute in interface TaskExecutor
Parameters:
task - the task to be executed
Throws:
IllegalArgumentException - if the supplied task is null, or if the Timer encapsulated by this instance has not been set, or if the configured delay is negative.

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()

The Spring Framework

Copyright © 2002-2008 The Spring Framework.