Class SimpleRetryPolicy

java.lang.Object
org.springframework.retry.policy.SimpleRetryPolicy
All Implemented Interfaces:
Serializable, RetryPolicy
Direct Known Subclasses:
ExpressionRetryPolicy

public class SimpleRetryPolicy extends Object implements RetryPolicy
Simple retry policy that retries a fixed number of times for a set of named exceptions (and subclasses). The number of attempts includes the initial try, so e.g.
 retryTemplate = new RetryTemplate(new SimpleRetryPolicy(3));
 retryTemplate.execute(callback);
 
will execute the callback at least once, and as many as 3 times.

Since version 1.3 it is not necessary to use this class. The same behaviour can be achieved by constructing a CompositeRetryPolicy with MaxAttemptsRetryPolicy and BinaryExceptionClassifierRetryPolicy inside, that is actually performed by:

 RetryTemplate.builder()
                  .maxAttempts(3)
                  .retryOn(Exception.class)
                  .build();
 
or by RetryTemplate.defaultInstance()
Author:
Dave Syer, Rob Harrop, Gary Russell, Aleksandr Shamukov, Artem Bilan, Emanuele Ivaldi
See Also:
  • Field Details

    • DEFAULT_MAX_ATTEMPTS

      public static final int DEFAULT_MAX_ATTEMPTS
      The default limit to the number of attempts for a new policy.
      See Also:
  • Constructor Details

    • SimpleRetryPolicy

      public SimpleRetryPolicy()
      Create a SimpleRetryPolicy with the default number of retry attempts, retrying all exceptions.
    • SimpleRetryPolicy

      public SimpleRetryPolicy(int maxAttempts)
      Create a SimpleRetryPolicy with the specified number of retry attempts, retrying all exceptions.
      Parameters:
      maxAttempts - the maximum number of attempts
    • SimpleRetryPolicy

      public SimpleRetryPolicy(int maxAttempts, Map<Class<? extends Throwable>,Boolean> retryableExceptions)
      Create a SimpleRetryPolicy with the specified number of retry attempts.
      Parameters:
      maxAttempts - the maximum number of attempts
      retryableExceptions - the map of exceptions that are retryable
    • SimpleRetryPolicy

      public SimpleRetryPolicy(int maxAttempts, Map<Class<? extends Throwable>,Boolean> retryableExceptions, boolean traverseCauses)
      Create a SimpleRetryPolicy with the specified number of retry attempts. If traverseCauses is true, the exception causes will be traversed until a match or the root cause is found.
      Parameters:
      maxAttempts - the maximum number of attempts
      retryableExceptions - the map of exceptions that are retryable based on the map value (true/false).
      traverseCauses - true to traverse the exception cause chain until a classified exception is found or the root cause is reached.
    • SimpleRetryPolicy

      public SimpleRetryPolicy(int maxAttempts, Map<Class<? extends Throwable>,Boolean> retryableExceptions, boolean traverseCauses, boolean defaultValue)
      Create a SimpleRetryPolicy with the specified number of retry attempts. If traverseCauses is true, the exception causes will be traversed until a match or the root cause is found. The default value indicates whether to retry or not for exceptions (or super classes thereof) that are not found in the map.
      Parameters:
      maxAttempts - the maximum number of attempts
      retryableExceptions - the map of exceptions that are retryable based on the map value (true/false).
      traverseCauses - true to traverse the exception cause chain until a classified exception is found or the root cause is reached.
      defaultValue - the default action.
    • SimpleRetryPolicy

      public SimpleRetryPolicy(int maxAttempts, BinaryExceptionClassifier classifier)
      Create a SimpleRetryPolicy with the specified number of retry attempts and provided exception classifier.
      Parameters:
      maxAttempts - the maximum number of attempts
      classifier - custom exception classifier
  • Method Details

    • setMaxAttempts

      public void setMaxAttempts(int maxAttempts)
      Set the number of attempts before retries are exhausted. Includes the initial attempt before the retries begin so, generally, will be >= 1. For example setting this property to 3 means 3 attempts total (initial + 2 retries).
      Parameters:
      maxAttempts - the maximum number of attempts including the initial attempt.
    • setNotRecoverable

      public void setNotRecoverable(Class<? extends Throwable>... noRecovery)
      Configure throwables that should not be passed to a recoverer (if present) but thrown immediately.
      Parameters:
      noRecovery - the throwables.
      Since:
      3.0
    • maxAttemptsSupplier

      public void maxAttemptsSupplier(Supplier<Integer> maxAttemptsSupplier)
      Set a supplier for the number of attempts before retries are exhausted. Includes the initial attempt before the retries begin so, generally, will be >= 1. For example setting this property to 3 means 3 attempts total (initial + 2 retries). IMPORTANT: This policy cannot be serialized when a max attempts supplier is provided. Serialization might be used by a distributed cache when using this policy in a CircuitBreaker context.
      Parameters:
      maxAttemptsSupplier - the maximum number of attempts including the initial attempt.
      Since:
      2.0
    • getMaxAttempts

      public int getMaxAttempts()
      The maximum number of attempts before failure.
      Specified by:
      getMaxAttempts in interface RetryPolicy
      Returns:
      the maximum number of attempts
    • canRetry

      public boolean canRetry(RetryContext context)
      Test for retryable operation based on the status.
      Specified by:
      canRetry in interface RetryPolicy
      Parameters:
      context - the current retry status
      Returns:
      true if the last exception was retryable and the number of attempts so far is less than the limit.
      See Also:
    • close

      public void close(RetryContext status)
      Specified by:
      close in interface RetryPolicy
      Parameters:
      status - a retry status created by the RetryPolicy.open(RetryContext) method of this policy.
      See Also:
    • registerThrowable

      public void registerThrowable(RetryContext context, Throwable throwable)
      Update the status with another attempted retry and the latest exception.
      Specified by:
      registerThrowable in interface RetryPolicy
      Parameters:
      context - the current status object.
      throwable - the exception to throw
      See Also:
    • open

      public RetryContext open(RetryContext parent)
      Get a status object that can be used to track the current operation according to this policy. Has to be aware of the latest exception and the number of attempts.
      Specified by:
      open in interface RetryPolicy
      Parameters:
      parent - the parent context if we are in a nested retry.
      Returns:
      a RetryContext object specific to this policy.
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object