Class DelayHandler
- All Implemented Interfaces:
- EventListener,- org.reactivestreams.Subscriber<Message<?>>,- Aware,- BeanClassLoaderAware,- BeanFactoryAware,- BeanNameAware,- DisposableBean,- InitializingBean,- ApplicationContextAware,- ApplicationListener<ContextRefreshedEvent>,- Ordered,- ComponentSourceAware,- ExpressionCapable,- Orderable,- MessageProducer,- DelayHandlerManagement,- HeaderPropagationAware,- IntegrationPattern,- NamedComponent,- IntegrationManagement,- TrackableComponent,- MessageHandler,- reactor.core.CoreSubscriber<Message<?>>
MessageHandler that is capable of delaying the continuation of a Message flow
 based on the result of evaluation delayExpression 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 delayExpression property is configured, that evaluation
 result value will take precedence over the handler's defaultDelay value. The
 actual evaluation result 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()).
 
 Delayed messages are stored in the MessageGroupStore as a dedicated group.
 If an external persistent store is provided, those delayed messages will be rescheduled
 after application startup.
 The messageGroupId is required option and must be unique for each delayer
 configuration to avoid work-stealing from the store and unexpected releases.
 Different instances of the same delayer can point to the same message group in the store.
 The messageGroupId cannot rely on a bean name which might be generated.
 After application restart the bean may get a different generated name and its delayed
 messages might be lost from reschedule since its group is not managed
 by the application anymore.
- Since:
- 1.0.3
- Author:
- Mark Fisher, Artem Bilan, Gary Russell, Christian Tzolov, Youbin Wu
- 
Nested Class SummaryNested ClassesNested classes/interfaces inherited from class org.springframework.integration.handler.AbstractReplyProducingMessageHandlerAbstractReplyProducingMessageHandler.RequestHandlerNested classes/interfaces inherited from interface org.springframework.integration.support.management.IntegrationManagementIntegrationManagement.ManagementOverrides
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final intstatic final longFields inherited from class org.springframework.integration.handler.AbstractMessageProducingHandlermessagingTemplateFields inherited from class org.springframework.integration.context.IntegrationObjectSupportEXPRESSION_PARSER, loggerFields inherited from interface org.springframework.integration.support.management.IntegrationManagementMETER_PREFIX, RECEIVE_COUNTER_NAME, SEND_TIMER_NAMEFields inherited from interface org.springframework.core.OrderedHIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
- 
Constructor SummaryConstructorsConstructorDescriptionConstruct an instance with default options.DelayHandler(String messageGroupId) Create a DelayHandler with the given 'messageGroupId' that is used as 'key' forMessageGroupto store delayed Messages in theMessageGroupStore.DelayHandler(String messageGroupId, TaskScheduler taskScheduler) Create a DelayHandler with the given default delay.
- 
Method SummaryModifier and TypeMethodDescriptionprotected voiddoInit()intReturn a pattern type this component implements.protected @Nullable ObjecthandleRequestMessage(Message<?> requestMessage) Check if 'requestMessage' wasn't delayed before (releaseMessageAfterDelay(org.springframework.messaging.Message<?>, long)andDelayHandler.DelayedMessageWrapper).voidHandleContextRefreshedEventto invokereschedulePersistedMessages()as late as possible after application context startup.protected voidrescheduleAt(Message<?> message, Date startTime) voidUsed for reading persisted Messages in the 'messageStore' to reschedule them e.g.voidsetDefaultDelay(long defaultDelay) Set the default delay in milliseconds.voidsetDelayedAdviceChain(List<Advice> delayedAdviceChain) Specify theList<Advice>to adviseDelayHandler.ReleaseMessageHandlerproxy.voidsetDelayedMessageErrorChannel(MessageChannel delayedMessageErrorChannel) Set a message channel to which anErrorMessagewill be sent if sending the released message fails.voidsetDelayedMessageErrorChannelName(String delayedMessageErrorChannelName) Set a message channel name to which anErrorMessagewill be sent if sending the released message fails.voidsetDelayExpression(Expression delayExpression) Specify theExpressionthat should be checked for a delay period (in milliseconds) or a Date to delay until.voidsetDelayExpressionString(String delayExpression) Specify theExpressionthat should be checked for a delay period (in milliseconds) or a Date to delay until.voidsetIgnoreExpressionFailures(boolean ignoreExpressionFailures) Specify whetherExceptionsthrown bydelayExpressionevaluation should be ignored (only logged).voidsetMaxAttempts(int maxAttempts) Set the maximum number of release attempts for when the message release fails.voidsetMessageGroupId(String messageGroupId) Set a group id to manage delayed messages by this handler.voidsetMessageStore(MessageGroupStore messageStore) Specify theMessageGroupStorethat should be used to store Messages while awaiting the delay.voidsetRetryDelay(long retryDelay) Set an additional delay to apply when retrying after a release failure.protected booleanSubclasses may override this.Methods inherited from class org.springframework.integration.handler.AbstractReplyProducingMessageHandlerdoInvokeAdvisedRequestHandler, getBeanClassLoader, getRequiresReply, handleMessageInternal, hasAdviceChain, onInit, setAdviceChain, setBeanClassLoader, setRequiresReplyMethods inherited from class org.springframework.integration.handler.AbstractMessageProducingHandleraddNotPropagatedHeaders, createOutputMessage, getNotPropagatedHeaders, getOutputChannel, isAsync, messageBuilderForReply, produceOutput, resolveErrorChannel, sendErrorMessage, sendOutput, sendOutputs, setAsync, setNotPropagatedHeaders, setOutputChannel, setOutputChannelName, setSendTimeout, setupMessageProcessor, shouldSplitOutput, updateNotPropagatedHeadersMethods inherited from class org.springframework.integration.handler.AbstractMessageHandlerhandleMessage, onComplete, onError, onNext, onSubscribe, setObservationConventionMethods inherited from class org.springframework.integration.handler.MessageHandlerSupportbuildSendTimer, destroy, getManagedName, getManagedType, getMetricsCaptor, getObservationRegistry, getOrder, getOverrides, isLoggingEnabled, isObserved, registerMetricsCaptor, registerObservationRegistry, sendTimer, setLoggingEnabled, setManagedName, setManagedType, setOrder, setShouldTrack, shouldTrackMethods inherited from class org.springframework.integration.context.IntegrationObjectSupportafterPropertiesSet, extractTypeIfPossible, generateId, getApplicationContext, getApplicationContextId, getBeanDescription, getBeanFactory, getBeanName, getChannelResolver, getComponentDescription, getComponentName, getComponentSource, getConversionService, getExpression, getIntegrationProperties, getMessageBuilderFactory, getTaskScheduler, isInitialized, setApplicationContext, setBeanFactory, setBeanName, setChannelResolver, setComponentDescription, setComponentName, setComponentSource, setConversionService, setMessageBuilderFactory, setPrimaryExpression, setTaskScheduler, toStringMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.springframework.context.ApplicationListenersupportsAsyncExecutionMethods inherited from interface reactor.core.CoreSubscribercurrentContextMethods inherited from interface org.springframework.integration.support.management.IntegrationManagementgetThisAsMethods inherited from interface org.springframework.integration.support.context.NamedComponentgetBeanName, getComponentName
- 
Field Details- 
DEFAULT_MAX_ATTEMPTSpublic static final int DEFAULT_MAX_ATTEMPTS- See Also:
 
- 
DEFAULT_RETRY_DELAYpublic static final long DEFAULT_RETRY_DELAY- See Also:
 
 
- 
- 
Constructor Details- 
DelayHandlerpublic DelayHandler()Construct an instance with default options. ThemessageGroupIdmust then be provided via the setter.- Since:
- 6.2
 
- 
DelayHandlerCreate a DelayHandler with the given 'messageGroupId' that is used as 'key' forMessageGroupto store delayed Messages in theMessageGroupStore. The sending of Messages after the delay will be handled by registered in the ApplicationContext defaultThreadPoolTaskScheduler.- Parameters:
- messageGroupId- The message group identifier.
- See Also:
 
- 
DelayHandlerCreate a DelayHandler with the given default delay. The sending of Messages after the delay will be handled by the providedTaskScheduler.- Parameters:
- messageGroupId- The message group identifier.
- taskScheduler- A task scheduler.
 
 
- 
- 
Method Details- 
setMessageGroupIdSet a group id to manage delayed messages by this handler. Required.- Parameters:
- messageGroupId- the group id for delayed messages.
- Since:
- 6.2
 
- 
setDefaultDelaypublic void setDefaultDelay(long defaultDelay) Set the default delay in milliseconds. If nodelayExpressionproperty has been provided, the default delay will be applied to all Messages. If a delay should only be applied to Messages with evaluation result fromdelayExpression, then set this value to 0.- Parameters:
- defaultDelay- The default delay in milliseconds.
 
- 
setDelayExpressionSpecify theExpressionthat should be checked for a delay period (in milliseconds) or a Date to delay until. If this property is set, the result of the expression evaluation (if not null) will take precedence over this handler's default delay.- Parameters:
- delayExpression- The delay expression.
 
- 
setDelayExpressionStringSpecify theExpressionthat should be checked for a delay period (in milliseconds) or a Date to delay until. If this property is set, the result of the expression evaluation (if not null) will take precedence over this handler's default delay.- Parameters:
- delayExpression- The delay expression.
- Since:
- 5.0
 
- 
setIgnoreExpressionFailurespublic void setIgnoreExpressionFailures(boolean ignoreExpressionFailures) Specify whetherExceptionsthrown bydelayExpressionevaluation should be ignored (only logged). In this case the delayer will fall back to thedefaultDelay. If this property is specified asfalse, anydelayExpressionevaluationExceptionwill be thrown to the caller without falling back to thedefaultDelay. Default istrue.- Parameters:
- ignoreExpressionFailures- true if expression evaluation failures should be ignored.
- See Also:
 
- 
setMessageStoreSpecify theMessageGroupStorethat should be used to store Messages while awaiting the delay.- Parameters:
- messageStore- The message store.
 
- 
setDelayedAdviceChainSpecify theList<Advice>to adviseDelayHandler.ReleaseMessageHandlerproxy. Usually used to add transactions to delayed messages retrieved from a transactional message store.- Parameters:
- delayedAdviceChain- The advice chain.
- See Also:
 
- 
setDelayedMessageErrorChannelSet a message channel to which anErrorMessagewill be sent if sending the released message fails. If the error flow returns normally, the release is complete. If the error flow throws an exception, the release will be re-attempted. If there is a transaction advice on the release task, the error flow is called within the transaction.- Parameters:
- delayedMessageErrorChannel- the channel.
- Since:
- 5.0.8
- See Also:
 
- 
setDelayedMessageErrorChannelNameSet a message channel name to which anErrorMessagewill be sent if sending the released message fails. If the error flow returns normally, the release is complete. If the error flow throws an exception, the release will be re-attempted. If there is a transaction advice on the release task, the error flow is called within the transaction.- Parameters:
- delayedMessageErrorChannelName- the channel name.
- Since:
- 5.0.8
- See Also:
 
- 
setMaxAttemptspublic void setMaxAttempts(int maxAttempts) Set the maximum number of release attempts for when the message release fails. Default 5.- Parameters:
- maxAttempts- the max attempts.
- Since:
- 5.0.8
- See Also:
 
- 
setRetryDelaypublic void setRetryDelay(long retryDelay) Set an additional delay to apply when retrying after a release failure. Default 1000L.- Parameters:
- retryDelay- the retry delay.
- Since:
- 5.0.8
- See Also:
 
- 
getComponentType- Specified by:
- getComponentTypein interface- NamedComponent
- Overrides:
- getComponentTypein class- MessageHandlerSupport
 
- 
getIntegrationPatternTypeDescription copied from interface:IntegrationPatternReturn a pattern type this component implements.- Specified by:
- getIntegrationPatternTypein interface- IntegrationPattern
- Overrides:
- getIntegrationPatternTypein class- AbstractReplyProducingMessageHandler
- Returns:
- the IntegrationPatternTypethis component implements.
 
- 
doInitprotected void doInit()- Overrides:
- doInitin class- AbstractReplyProducingMessageHandler
 
- 
shouldCopyRequestHeadersprotected boolean shouldCopyRequestHeaders()Description copied from class:AbstractMessageProducingHandlerSubclasses may override this. True by default.- Overrides:
- shouldCopyRequestHeadersin class- AbstractMessageProducingHandler
- Returns:
- true if the request headers should be copied.
 
- 
handleRequestMessageCheck if 'requestMessage' wasn't delayed before (releaseMessageAfterDelay(org.springframework.messaging.Message<?>, long)andDelayHandler.DelayedMessageWrapper). Then determine 'delay' for 'requestMessage' (determineDelayForMessage(org.springframework.messaging.Message<?>)) and ifdelay > 0schedules 'releaseMessage' task after 'delay'.- Specified by:
- handleRequestMessagein class- AbstractReplyProducingMessageHandler
- Parameters:
- requestMessage- - the Message which may be delayed.
- Returns:
- - nullif 'requestMessage' is delayed, otherwise - 'payload' from 'requestMessage'.
- See Also:
 
- 
rescheduleAt
- 
getDelayedMessageCountpublic int getDelayedMessageCount()- Specified by:
- getDelayedMessageCountin interface- DelayHandlerManagement
 
- 
reschedulePersistedMessagespublic void reschedulePersistedMessages()Used for reading persisted Messages in the 'messageStore' to reschedule them e.g. upon application restart. The logic is based on iteration overmessageGroup.getMessages()and schedules a task for 'delay' logic. This behavior is dictated by the avoidance of invocation thread overload.- Specified by:
- reschedulePersistedMessagesin interface- DelayHandlerManagement
 
- 
onApplicationEventHandleContextRefreshedEventto invokereschedulePersistedMessages()as late as possible after application context startup. Also, it checksinitializedto ignore otherContextRefreshedEvents which may be published in the 'parent-child' contexts, e.g. in the Spring-MVC applications.- Specified by:
- onApplicationEventin interface- ApplicationListener<ContextRefreshedEvent>
- Parameters:
- event- -- ContextRefreshedEventwhich occurs after Application context is completely initialized.
- See Also:
 
 
-