Class RequestHandlerRetryAdvice

All Implemented Interfaces:
Advice, Interceptor, MethodInterceptor, Aware, BeanFactoryAware, BeanNameAware, InitializingBean, ApplicationContextAware, ComponentSourceAware, ExpressionCapable, NamedComponent

public class RequestHandlerRetryAdvice extends AbstractRequestHandlerAdvice
The 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
  • Constructor Details

    • RequestHandlerRetryAdvice

      public RequestHandlerRetryAdvice()
  • Method Details

    • setRetryPolicy

      public void setRetryPolicy(org.springframework.core.retry.RetryPolicy retryPolicy)
      Set a RetryPolicy 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 a RetryListener to track retry cycle phases.
      Parameters:
      retryListener - the listener to use.
    • setRecoveryCallback

      public void setRecoveryCallback(RecoveryCallback<Object> recoveryCallback)
      Set a RecoveryCallback to handle a RetryException.
      Parameters:
      recoveryCallback - the callback to use.
    • setStateKeyFunction

      public void setStateKeyFunction(Function<Message<?>, Object> stateKeyFunction)
      Set a Function 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 when setStateKeyFunction(Function) is provided.
      Parameters:
      stateCacheSize - the size of the cache.
    • setNewMessagePredicate

      public void setNewMessagePredicate(Predicate<Message<?>> newMessagePredicate)
      Set a Predicate 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 when setStateKeyFunction(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 the MessageHandler.

      callback.execute() invokes the handler method and returns its result, or null.

      Specified by:
      doInvoke in class AbstractRequestHandlerAdvice
      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.