Class RetryPolicy.Builder

java.lang.Object
org.springframework.core.retry.RetryPolicy.Builder
Enclosing interface:
RetryPolicy

public static final class RetryPolicy.Builder extends Object
Fluent API for configuring a RetryPolicy with common configuration options.
Since:
7.0
Author:
Sam Brannen, Mahmoud Ben Hassine
  • Field Details

  • Method Details

    • backOff

      public RetryPolicy.Builder backOff(BackOff backOff)
      Specify the BackOff strategy to use.

      The supplied value will override any previously configured value.

      WARNING: If you configure a custom BackOff strategy, you should not configure any of the following: maxAttempts, delay, jitter, multiplier, or maxDelay.

      Parameters:
      backOff - the BackOff strategy
      Returns:
      this Builder instance for chained method invocations
    • maxAttempts

      public RetryPolicy.Builder maxAttempts(long maxAttempts)
      Specify the maximum number of retry attempts.

      The default is 3L.

      The supplied value will override any previously configured value.

      You should not specify this configuration option if you have configured a custom BackOff strategy.

      Parameters:
      maxAttempts - the maximum number of retry attempts; must be positive (or zero for no retry)
      Returns:
      this Builder instance for chained method invocations
    • delay

      public RetryPolicy.Builder delay(Duration delay)
      Specify the base delay after the initial invocation.

      If a multiplier is specified, this serves as the initial delay to multiply from.

      The default is 1000L milliseconds.

      The supplied value will override any previously configured value.

      You should not specify this configuration option if you have configured a custom BackOff strategy.

      Parameters:
      delay - the base delay, typically in milliseconds or seconds; must be greater than or equal to zero
      Returns:
      this Builder instance for chained method invocations
      See Also:
    • jitter

      public RetryPolicy.Builder jitter(Duration jitter)
      Specify a jitter value for the base retry attempt, randomly subtracted or added to the calculated delay, resulting in a value between delay - jitter and delay + jitter but never below the base delay or above the max delay.

      If a multiplier is specified, it is applied to the jitter value as well.

      The default is no jitter.

      The supplied value will override any previously configured value.

      You should not specify this configuration option if you have configured a custom BackOff strategy.

      Parameters:
      jitter - the jitter value, typically in milliseconds; must be greater than or equal to zero
      Returns:
      this Builder instance for chained method invocations
      See Also:
    • multiplier

      public RetryPolicy.Builder multiplier(double multiplier)
      Specify a multiplier for a delay for the next retry attempt, applied to the previous delay (starting with the initial delay) as well as to the applicable jitter for each attempt.

      The default is 1.0, effectively resulting in a fixed delay.

      The supplied value will override any previously configured value.

      You should not specify this configuration option if you have configured a custom BackOff strategy.

      Parameters:
      multiplier - the multiplier value; must be greater than or equal to 1
      Returns:
      this Builder instance for chained method invocations
      See Also:
    • maxDelay

      public RetryPolicy.Builder maxDelay(Duration maxDelay)
      Specify the maximum delay for any retry attempt, limiting how far jitter and the multiplier can increase the delay.

      The default is unlimited.

      The supplied value will override any previously configured value.

      You should not specify this configuration option if you have configured a custom BackOff strategy.

      Parameters:
      maxDelay - the maximum delay; must be greater than zero
      Returns:
      this Builder instance for chained method invocations
      See Also:
    • includes

      @SafeVarargs public final RetryPolicy.Builder includes(Class<? extends Throwable>... types)
      Specify the types of exceptions for which the RetryPolicy should retry a failed operation.

      Defaults to all exception types.

      The supplied exception types will be matched against an exception thrown by a failed operation as well as nested causes.

      If included exception types have already been configured, the supplied types will be added to the existing list of included types.

      This can be combined with other includes, excludes, and a custom predicate.

      Parameters:
      types - the types of exceptions to include in the policy
      Returns:
      this Builder instance for chained method invocations
      See Also:
    • includes

      public RetryPolicy.Builder includes(Collection<Class<? extends Throwable>> types)
      Specify the types of exceptions for which the RetryPolicy should retry a failed operation.

      Defaults to all exception types.

      The supplied exception types will be matched against an exception thrown by a failed operation as well as nested causes.

      If included exception types have already been configured, the supplied types will be added to the existing list of included types.

      This can be combined with other includes, excludes, and a custom predicate.

      Parameters:
      types - the types of exceptions to include in the policy
      Returns:
      this Builder instance for chained method invocations
      See Also:
    • excludes

      @SafeVarargs public final RetryPolicy.Builder excludes(Class<? extends Throwable>... types)
      Specify the types of exceptions for which the RetryPolicy should not retry a failed operation.

      The supplied exception types will be matched against an exception thrown by a failed operation as well as nested causes.

      If excluded exception types have already been configured, the supplied types will be added to the existing list of excluded types.

      This can be combined with includes, other excludes, and a custom predicate.

      Parameters:
      types - the types of exceptions to exclude from the policy
      Returns:
      this Builder instance for chained method invocations
      See Also:
    • excludes

      public RetryPolicy.Builder excludes(Collection<Class<? extends Throwable>> types)
      Specify the types of exceptions for which the RetryPolicy should not retry a failed operation.

      The supplied exception types will be matched against an exception thrown by a failed operation as well as nested causes.

      If excluded exception types have already been configured, the supplied types will be added to the existing list of excluded types.

      This can be combined with includes, other excludes, and a custom predicate.

      Parameters:
      types - the types of exceptions to exclude from the policy
      Returns:
      this Builder instance for chained method invocations
      See Also:
    • predicate

      public RetryPolicy.Builder predicate(Predicate<Throwable> predicate)
      Specify a custom Predicate that the RetryPolicy will use to determine whether to retry a failed operation based on a given Throwable.

      If a predicate has already been configured, the supplied predicate will be combined with the existing predicate.

      This can be combined with includes and excludes.

      Parameters:
      predicate - a custom predicate
      Returns:
      this Builder instance for chained method invocations
      See Also:
    • build

      public RetryPolicy build()
      Build the configured RetryPolicy.