Class MaxAttemptsRetryPolicy

java.lang.Object
org.springframework.retry.policy.MaxAttemptsRetryPolicy
All Implemented Interfaces:
Serializable, RetryPolicy

public class MaxAttemptsRetryPolicy extends Object implements RetryPolicy
Simple retry policy that is aware only about attempt count and retries a fixed number of times. The number of attempts includes the initial try.

It is not recommended to use it directly, because usually exception classification is strongly recommended (to not retry on OutOfMemoryError, for example).

For daily usage see RetryTemplate.builder()

Volatility of maxAttempts allows concurrent modification and does not require safe publication of new instance after construction.

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

    • MaxAttemptsRetryPolicy

      public MaxAttemptsRetryPolicy()
      Create a MaxAttemptsRetryPolicy with the default number of retry attempts (3), retrying all throwables.
    • MaxAttemptsRetryPolicy

      public MaxAttemptsRetryPolicy(int maxAttempts)
      Create a MaxAttemptsRetryPolicy with the specified number of retry attempts, retrying all throwables.
      Parameters:
      maxAttempts - the maximum number of attempts
  • 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.
    • 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.
    • 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: