Spring Integration

org.springframework.integration.handler
Class DelayHandler

java.lang.Object
  extended by org.springframework.integration.context.IntegrationObjectSupport
      extended by org.springframework.integration.handler.DelayHandler
All Implemented Interfaces:
org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.core.Ordered, NamedComponent, MessageHandler, MessageProducer

public class DelayHandler
extends IntegrationObjectSupport
implements MessageHandler, MessageProducer, org.springframework.core.Ordered, org.springframework.beans.factory.DisposableBean

A MessageHandler that is capable of delaying the continuation of a Message flow based on the presence of a delay header on an inbound Message or a default delay value configured on this handler. Note that the continuation of the flow is delegated to a TaskScheduler, and therefore, the calling thread does not block. The advantage of this approach is that many delays can be managed concurrently, even very long delays, without producing a buildup of blocked Threads.

One thing to keep in mind, however, is that any active transactional context will not propagate from the original sender to the eventual recipient. This is a side-effect of passing the Message to the output channel after the delay with a different Thread in control.

When this handler's 'delayHeaderName' property is configured, that value, if present on a Message, will take precedence over the handler's 'defaultDelay' value. The actual header value may be a long, a String that can be parsed as a long, or a Date. If it is a long, it will be interpreted as the length of time to delay in milliseconds counting from the current time (e.g. a value of 5000 indicates that the Message can be released as soon as five seconds from the current time). If the value is a Date, it will be delayed at least until that Date occurs (i.e. the delay in that case is equivalent to headerDate.getTime() - new Date().getTime()).

Since:
1.0.3
Author:
Mark Fisher

Field Summary
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
DelayHandler(long defaultDelay)
          Create a DelayHandler with the given default delay.
DelayHandler(long defaultDelay, org.springframework.scheduling.TaskScheduler taskScheduler)
          Create a DelayHandler with the given default delay.
 
Method Summary
 void destroy()
           
 java.lang.String getComponentType()
          Subclasses may implement this method to provide component type information.
 int getOrder()
           
 void handleMessage(Message<?> message)
          Handles the message if possible.
protected  void onInit()
          Subclasses may implement this for initialization logic.
 void setDefaultDelay(long defaultDelay)
          Set the default delay in milliseconds.
 void setDelayHeaderName(java.lang.String delayHeaderName)
          Specify the name of the header that should be checked for a delay period (in milliseconds) or a Date to delay until.
 void setMessageStore(MessageStore messageStore)
          Specify the MessageStore that should be used to store Messages while awaiting the delay.
 void setOrder(int order)
           
 void setOutputChannel(MessageChannel outputChannel)
          Set the output channel for this handler.
 void setSendTimeout(long sendTimeout)
          Set the timeout for sending reply Messages.
 void setWaitForTasksToCompleteOnShutdown(boolean waitForJobsToCompleteOnShutdown)
          Set whether to wait for scheduled tasks to complete on shutdown.
 
Methods inherited from class org.springframework.integration.context.IntegrationObjectSupport
afterPropertiesSet, getBeanFactory, getComponentName, getConversionService, getTaskScheduler, setBeanFactory, setBeanName, setComponentName, setConversionService, setTaskScheduler, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DelayHandler

public DelayHandler(long defaultDelay)
Create a DelayHandler with the given default delay. The sending of Messages after the delay will be handled by a scheduled thread pool with a size of 1.


DelayHandler

public DelayHandler(long defaultDelay,
                    org.springframework.scheduling.TaskScheduler taskScheduler)
Create a DelayHandler with the given default delay. The sending of Messages after the delay will be handled by the provided TaskScheduler.

Method Detail

setDefaultDelay

public void setDefaultDelay(long defaultDelay)
Set the default delay in milliseconds. If no 'delayHeaderName' property has been provided, the default delay will be applied to all Messages. If a delay should only be applied to Messages with a header, then set this value to 0.


setDelayHeaderName

public void setDelayHeaderName(java.lang.String delayHeaderName)
Specify the name of the header that should be checked for a delay period (in milliseconds) or a Date to delay until. If this property is set, any such header value will take precedence over this handler's default delay.


setMessageStore

public void setMessageStore(MessageStore messageStore)
Specify the MessageStore that should be used to store Messages while awaiting the delay.


setOutputChannel

public void setOutputChannel(MessageChannel outputChannel)
Set the output channel for this handler. If none is provided, each inbound Message must include a reply channel header.

Specified by:
setOutputChannel in interface MessageProducer

setSendTimeout

public void setSendTimeout(long sendTimeout)
Set the timeout for sending reply Messages.


setWaitForTasksToCompleteOnShutdown

public void setWaitForTasksToCompleteOnShutdown(boolean waitForJobsToCompleteOnShutdown)
Set whether to wait for scheduled tasks to complete on shutdown.

Default is "false". Switch this to "true" if you prefer fully completed tasks at the expense of a longer shutdown phase.

This property will only have an effect for TaskScheduler implementations that extend from ExecutorConfigurationSupport.

See Also:
ExecutorConfigurationSupport.setWaitForTasksToCompleteOnShutdown(boolean)

setOrder

public void setOrder(int order)

getOrder

public int getOrder()
Specified by:
getOrder in interface org.springframework.core.Ordered

getComponentType

public java.lang.String getComponentType()
Description copied from class: IntegrationObjectSupport
Subclasses may implement this method to provide component type information.

Specified by:
getComponentType in interface NamedComponent
Overrides:
getComponentType in class IntegrationObjectSupport

onInit

protected void onInit()
               throws java.lang.Exception
Description copied from class: IntegrationObjectSupport
Subclasses may implement this for initialization logic.

Overrides:
onInit in class IntegrationObjectSupport
Throws:
java.lang.Exception

handleMessage

public final void handleMessage(Message<?> message)
Description copied from interface: MessageHandler
Handles the message if possible. If the handler cannot deal with the message this will result in a MessageRejectedException e.g. in case of a Selective Consumer. When a consumer tries to handle a message, but fails to do so, a MessageHandlingException is thrown. In the last case it is recommended to treat the message as tainted and go into an error scenario.

When the handling results in a failure of another message being sent (e.g. a "reply" message), that failure will trigger a MessageDeliveryException.

Specified by:
handleMessage in interface MessageHandler
Parameters:
message - the message to be handled

destroy

public void destroy()
             throws java.lang.Exception
Specified by:
destroy in interface org.springframework.beans.factory.DisposableBean
Throws:
java.lang.Exception

Spring Integration