Class RequestHandlerRetryAdvice
- All Implemented Interfaces:
Advice
,Interceptor
,MethodInterceptor
,Aware
,BeanFactoryAware
,BeanNameAware
,InitializingBean
,ApplicationContextAware
,ComponentSourceAware
,ExpressionCapable
,NamedComponent
AbstractRequestHandlerAdvice
implementation for retrying the target
MessageHandler
execution.
By default, this advice performs 3 attempts (plus initial execution) with no delay in between.
If setStateKeyFunction(Function)
is provided, the retry logic is turned into a stateful algorithm.
In this case, a BackOffExecution
is cached by the mentioned retry state key if an exception is retryable.
Then this exception is thrown back to the caller, e.g.,
for transaction rollback or re-queueing the message onto message broker.
The next time when a message with the same key arrives again (redelivered),
the cached BackOffExecution
is restored, and the target service is called again
immediately according to the RetryPolicy
.
If the setNewMessagePredicate(Predicate)
returns true
,
a new stateful retry cycle is started even if a state for this message is already cached.
- Since:
- 2.2
- Author:
- Gary Russell, Artem Bilan
-
Nested Class Summary
Nested classes/interfaces inherited from class org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice
AbstractRequestHandlerAdvice.ExecutionCallback, AbstractRequestHandlerAdvice.ThrowableHolderException
-
Field Summary
Fields inherited from class org.springframework.integration.context.IntegrationObjectSupport
EXPRESSION_PARSER, logger
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected @Nullable Object
doInvoke
(AbstractRequestHandlerAdvice.ExecutionCallback callback, Object target, Message<?> message) Subclasses implement this method to apply behavior to theMessageHandler
.void
setNewMessagePredicate
(Predicate<Message<?>> newMessagePredicate) Set aPredicate
to determine if a fresh stateful retry state should be started for this request message.void
setRecoveryCallback
(RecoveryCallback<Object> recoveryCallback) Set aRecoveryCallback
to handle aRetryException
.void
setRetryListener
(org.springframework.core.retry.RetryListener retryListener) Set aRetryListener
to track retry cycle phases.void
setRetryPolicy
(org.springframework.core.retry.RetryPolicy retryPolicy) Set aRetryPolicy
to use.void
setStateCacheSize
(int stateCacheSize) The size of the stateful retry state cache.void
setStateKeyFunction
(Function<Message<?>, Object> stateKeyFunction) Set aFunction
to determine a stateful retry execution key against a request message.Methods inherited from class org.springframework.integration.handler.advice.AbstractRequestHandlerAdvice
getComponentType, invoke, unwrapExceptionIfNecessary, unwrapThrowableIfNecessary
Methods inherited from class org.springframework.integration.context.IntegrationObjectSupport
afterPropertiesSet, extractTypeIfPossible, generateId, getApplicationContext, getApplicationContextId, getBeanDescription, getBeanFactory, getBeanName, getChannelResolver, getComponentDescription, getComponentName, getComponentSource, getConversionService, getExpression, getIntegrationProperties, getMessageBuilderFactory, getTaskScheduler, isInitialized, onInit, setApplicationContext, setBeanFactory, setBeanName, setChannelResolver, setComponentDescription, setComponentName, setComponentSource, setConversionService, setMessageBuilderFactory, setPrimaryExpression, setTaskScheduler, toString
-
Constructor Details
-
RequestHandlerRetryAdvice
public RequestHandlerRetryAdvice()
-
-
Method Details
-
setRetryPolicy
public void setRetryPolicy(org.springframework.core.retry.RetryPolicy retryPolicy) Set aRetryPolicy
to use. Defaults to 3 attempts with no delay in between.- Parameters:
retryPolicy
- the policy.
-
setRetryListener
public void setRetryListener(org.springframework.core.retry.RetryListener retryListener) Set aRetryListener
to track retry cycle phases.- Parameters:
retryListener
- the listener to use.
-
setRecoveryCallback
Set aRecoveryCallback
to handle aRetryException
.- Parameters:
recoveryCallback
- the callback to use.
-
setStateKeyFunction
Set aFunction
to determine a stateful retry execution key against a request message. If not provided, the retry behavior is stateless.- Parameters:
stateKeyFunction
- the function to determine a stateful retry execution key against a request message.
-
setStateCacheSize
public void setStateCacheSize(int stateCacheSize) The size of the stateful retry state cache. Defaults to 100. This option is used only whensetStateKeyFunction(Function)
is provided.- Parameters:
stateCacheSize
- the size of the cache.
-
setNewMessagePredicate
Set aPredicate
to determine if a fresh stateful retry state should be started for this request message.false
by default to not refresh existing stateful retry states. This option is used only whensetStateKeyFunction(Function)
is provided.- Parameters:
newMessagePredicate
- the predicate to use.
-
doInvoke
protected @Nullable Object doInvoke(AbstractRequestHandlerAdvice.ExecutionCallback callback, Object target, Message<?> message) Description copied from class:AbstractRequestHandlerAdvice
Subclasses implement this method to apply behavior to theMessageHandler
.callback.execute() invokes the handler method and returns its result, or null.
- Specified by:
doInvoke
in classAbstractRequestHandlerAdvice
- Parameters:
callback
- Subclasses invoke the execute() method on this interface to invoke the handler method.target
- The target handler.message
- The message that will be sent to the handler.- Returns:
- the result after invoking the
MessageHandler
.
-