Class RetryTemplate

java.lang.Object
org.springframework.core.retry.RetryTemplate
All Implemented Interfaces:
RetryOperations

public class RetryTemplate extends Object implements RetryOperations
A basic implementation of RetryOperations that executes and potentially retries a Retryable operation based on a configured RetryPolicy.

By default, a retryable operation will be executed once and potentially retried at most 3 times with a fixed backoff of 1 second.

A RetryListener can be registered to react to events published during key retry phases (before a retry attempt, after a retry attempt, etc.).

All retry actions performed by this template are logged at debug level, using "org.springframework.core.retry.RetryTemplate" as the log category.

Since:
7.0
Author:
Mahmoud Ben Hassine, Sam Brannen, Juergen Hoeller
See Also:
  • Constructor Details

    • RetryTemplate

      public RetryTemplate()
      Create a new RetryTemplate with maximum 3 retry attempts and a fixed backoff of 1 second.
      See Also:
    • RetryTemplate

      public RetryTemplate(RetryPolicy retryPolicy)
      Create a new RetryTemplate with the supplied RetryPolicy.
      Parameters:
      retryPolicy - the retry policy to use
  • Method Details

    • setRetryPolicy

      public void setRetryPolicy(RetryPolicy retryPolicy)
      Set the RetryPolicy to use.

      Defaults to RetryPolicy.withDefaults().

      Parameters:
      retryPolicy - the retry policy to use
      See Also:
    • getRetryPolicy

      public RetryPolicy getRetryPolicy()
      Return the current RetryPolicy that is in use with this template.
    • setRetryListener

      public void setRetryListener(RetryListener retryListener)
      Set the RetryListener to use.

      If multiple listeners are needed, use a CompositeRetryListener.

      Defaults to a no-op implementation.

      Parameters:
      retryListener - the retry listener to use
    • getRetryListener

      public RetryListener getRetryListener()
      Return the current RetryListener that is in use with this template.
    • execute

      public <R extends @Nullable Object> R execute(Retryable<R> retryable) throws RetryException
      Description copied from interface: RetryOperations
      Execute the given Retryable operation according to the RetryPolicy configured at the implementation level.

      If the Retryable succeeds, its result will be returned. Otherwise, a RetryException will be thrown to the caller. The RetryException will contain the last exception thrown by the Retryable operation as the cause and any exceptions from previous attempts as suppressed exceptions.

      Specified by:
      execute in interface RetryOperations
      Type Parameters:
      R - the type of the result
      Parameters:
      retryable - the Retryable to execute and retry if needed
      Returns:
      the successful result of the Retryable, if any
      Throws:
      RetryException - if the RetryPolicy is exhausted. Note that this exception represents a failure outcome and is not meant to be propagated; you will typically rather rethrow its cause (the last original exception thrown by the Retryable callback) or throw a custom business exception instead.
    • invoke

      public <R extends @Nullable Object> R invoke(Supplier<R> retryable)
      Description copied from interface: RetryOperations
      Invoke the given Supplier according to the RetryPolicy, returning a successful result or throwing the last Supplier exception to the caller in case of retry policy exhaustion.
      Specified by:
      invoke in interface RetryOperations
      Type Parameters:
      R - the type of the result
      Parameters:
      retryable - the Supplier to invoke and retry if needed
      Returns:
      the result of the Supplier
    • invoke

      public void invoke(Runnable retryable)
      Description copied from interface: RetryOperations
      Invoke the given Runnable according to the RetryPolicy, returning successfully or throwing the last Runnable exception to the caller in case of retry policy exhaustion.
      Specified by:
      invoke in interface RetryOperations
      Parameters:
      retryable - the Runnable to invoke and retry if needed