Class BackOffPolicyBuilder

java.lang.Object
org.springframework.retry.backoff.BackOffPolicyBuilder

public class BackOffPolicyBuilder extends Object
Fluent API for creating a BackOffPolicy based on given attributes. The delay values are expressed in milliseconds. If any provided value is less than one, the resulting policy will set it to one. The default policy is a FixedBackOffPolicy with a delay of 1000ms.

Examples:


 // Default FixedBackOffPolicy with 1000ms delay
 BackOffPolicyBuilder
                .newDefaultPolicy();

 // FixedBackOffPolicy
 BackOffPolicyBuilder
                .newBuilder()
                .delay(2000)
                .build();

 // UniformRandomBackOffPolicy
 BackOffPolicyBuilder
                .newBuilder()
                .delay(500)
                .maxDelay(1000)
                .build();

 // ExponentialBackOffPolicy
 BackOffPolicyBuilder
                .newBuilder()
                .delay(1000)
                .maxDelay(5000)
                .multiplier(2)
                .build();

 // ExponentialRandomBackOffPolicy with provided Sleeper
 BackOffPolicyBuilder
                .newBuilder()
                .delay(3000)
                .maxDelay(5000)
                .multiplier(1.5)
                .random(true)
                .sleeper(mySleeper)
                .build();
 

Not thread safe. Building should be performed in a single thread. The resulting BackOffPolicy however is expected to be thread-safe and designed for moderate load concurrent access.

Since:
1.3.3
Author:
Tomaz Fernandes, Aftab Shaikh
  • Method Details

    • newBuilder

      public static BackOffPolicyBuilder newBuilder()
      Creates a new BackOffPolicyBuilder instance.
      Returns:
      the builder instance
    • newDefaultPolicy

      public static BackOffPolicy newDefaultPolicy()
      Creates a new FixedBackOffPolicy instance with a delay of 1000ms.
      Returns:
      the back off policy instance
    • delay

      public BackOffPolicyBuilder delay(long delay)
      A canonical backoff period. Used as an initial value in the exponential case, and as a minimum value in the uniform case.
      Parameters:
      delay - the initial or canonical backoff period in milliseconds
      Returns:
      this
    • maxDelay

      public BackOffPolicyBuilder maxDelay(long maxDelay)
      The maximum wait in milliseconds between retries. If less than delay(long) then a default value is applied depending on the resulting policy.
      Parameters:
      maxDelay - the maximum wait between retries in milliseconds
      Returns:
      this
    • multiplier

      public BackOffPolicyBuilder multiplier(double multiplier)
      If positive, then used as a multiplier for generating the next delay for backoff.
      Parameters:
      multiplier - a multiplier to use to calculate the next backoff delay
      Returns:
      this
    • random

      public BackOffPolicyBuilder random(boolean random)
      In the exponential case (multiplier > 0) set this to true to have the backoff delays randomized, so that the maximum delay is multiplier times the previous delay and the distribution is uniform between the two values.
      Parameters:
      random - the flag to signal randomization is required
      Returns:
      this
    • sleeper

      public BackOffPolicyBuilder sleeper(Sleeper sleeper)
      The Sleeper instance to be used to back off. Policies default to ThreadWaitSleeper.
      Parameters:
      sleeper - the Sleeper instance
      Returns:
      this
    • delaySupplier

      public BackOffPolicyBuilder delaySupplier(Supplier<Long> delaySupplier)
      Set a supplier for the delay.
      Parameters:
      delaySupplier - the supplier.
      Returns:
      this
      Since:
      2.0
    • maxDelaySupplier

      public BackOffPolicyBuilder maxDelaySupplier(Supplier<Long> maxDelaySupplier)
      Set a supplier for the max delay.
      Parameters:
      maxDelaySupplier - the supplier.
      Returns:
      this
      Since:
      2.0
    • multiplierSupplier

      public BackOffPolicyBuilder multiplierSupplier(Supplier<Double> multiplierSupplier)
      Set a supplier for the multiplier.
      Parameters:
      multiplierSupplier - the supplier.
      Returns:
      this
      Since:
      2.0
    • randomSupplier

      public BackOffPolicyBuilder randomSupplier(Supplier<Boolean> randomSupplier)
      Set a supplier for the random.
      Parameters:
      randomSupplier - the supplier.
      Returns:
      this
      Since:
      2.0
    • build

      public BackOffPolicy build()
      Builds the BackOffPolicy with the given parameters.
      Returns:
      the BackOffPolicy instance