Class ExponentialBackOffPolicy

java.lang.Object
org.springframework.retry.backoff.ExponentialBackOffPolicy
All Implemented Interfaces:
BackOffPolicy, SleepingBackOffPolicy<ExponentialBackOffPolicy>
Direct Known Subclasses:
ExponentialRandomBackOffPolicy

public class ExponentialBackOffPolicy extends Object implements SleepingBackOffPolicy<ExponentialBackOffPolicy>
Implementation of BackOffPolicy that increases the back off period for each retry attempt in a given set up to a limit. This implementation is thread-safe and suitable for concurrent access. Modifications to the configuration do not affect any retry sets that are already in progress. The setInitialInterval(long) property controls the initial delay value for the first retry and the setMultiplier(double) property controls by how much the delay is increased for each subsequent attempt. The delay interval is capped at setMaxInterval(long).
Author:
Rob Harrop, Dave Syer, Gary Russell, Artem Bilan, Marius Lichtblau, Anton Aharkau
  • Field Details

    • logger

      protected final org.apache.commons.logging.Log logger
    • DEFAULT_INITIAL_INTERVAL

      public static final long DEFAULT_INITIAL_INTERVAL
      The default 'initialInterval' value - 100 millisecs. Coupled with the default 'multiplier' value this gives a useful initial spread of pauses for 1-5 retries.
      See Also:
    • DEFAULT_MAX_INTERVAL

      public static final long DEFAULT_MAX_INTERVAL
      The default maximum backoff time (30 seconds).
      See Also:
    • DEFAULT_MULTIPLIER

      public static final double DEFAULT_MULTIPLIER
      The default 'multiplier' value - value 2 (100% increase per backoff).
      See Also:
  • Constructor Details

    • ExponentialBackOffPolicy

      public ExponentialBackOffPolicy()
  • Method Details

    • setSleeper

      public void setSleeper(Sleeper sleeper)
      Public setter for the Sleeper strategy.
      Parameters:
      sleeper - the sleeper to set defaults to ThreadWaitSleeper.
    • withSleeper

      public ExponentialBackOffPolicy withSleeper(Sleeper sleeper)
      Description copied from interface: SleepingBackOffPolicy
      Clone the policy and return a new policy which uses the passed sleeper.
      Specified by:
      withSleeper in interface SleepingBackOffPolicy<ExponentialBackOffPolicy>
      Parameters:
      sleeper - Target to be invoked any time the backoff policy sleeps
      Returns:
      a clone of this policy which will have all of its backoff sleeps routed into the passed sleeper
    • newInstance

      protected ExponentialBackOffPolicy newInstance()
    • cloneValues

      protected void cloneValues(ExponentialBackOffPolicy target)
    • setInitialInterval

      public void setInitialInterval(long initialInterval)
      Set the initial sleep interval value. Default is 100 millisecond. Cannot be set to a value less than one.
      Parameters:
      initialInterval - the initial interval
    • setMultiplier

      public void setMultiplier(double multiplier)
      Set the multiplier value. Default is '2.0'. Hint: do not use values much in excess of 1.0 (or the backoff will get very long very fast).
      Parameters:
      multiplier - the multiplier
    • setMaxInterval

      public void setMaxInterval(long maxInterval)
      Setter for maximum back off period. Default is 30000 (30 seconds). the value will be reset to 1 if this method is called with a value less than 1. Set this to avoid infinite waits if backing off a large number of times (or if the multiplier is set too high).
      Parameters:
      maxInterval - in milliseconds.
    • initialIntervalSupplier

      public void initialIntervalSupplier(Supplier<Long> initialIntervalSupplier)
      Set the initial sleep interval value. Default supplier supplies 100 millisecond.
      Parameters:
      initialIntervalSupplier - the initial interval
      Since:
      2.0
    • multiplierSupplier

      public void multiplierSupplier(Supplier<Double> multiplierSupplier)
      Set the multiplier value. Default supplier supplies '2.0'. Hint: do not use values much in excess of 1.0 (or the backoff will get very long very fast).
      Parameters:
      multiplierSupplier - the multiplier
      Since:
      2.0
    • maxIntervalSupplier

      public void maxIntervalSupplier(Supplier<Long> maxIntervalSupplier)
      Setter for maximum back off period. Default is 30000 (30 seconds). the value will be reset to 1 if this method is called with a value less than 1. Set this to avoid infinite waits if backing off a large number of times (or if the multiplier is set too high).
      Parameters:
      maxIntervalSupplier - in milliseconds.
      Since:
      2.0
    • getInitialIntervalSupplier

      protected Supplier<Long> getInitialIntervalSupplier()
    • getMaxIntervalSupplier

      protected Supplier<Long> getMaxIntervalSupplier()
    • getMultiplierSupplier

      protected Supplier<Double> getMultiplierSupplier()
    • getInitialInterval

      public long getInitialInterval()
      The initial period to sleep on the first backoff.
      Returns:
      the initial interval
    • getMaxInterval

      public long getMaxInterval()
      The maximum interval to sleep for. Defaults to 30 seconds.
      Returns:
      the maximum interval.
    • getMultiplier

      public double getMultiplier()
      The multiplier to use to generate the next backoff interval from the last.
      Returns:
      the multiplier in use
    • start

      public BackOffContext start(RetryContext context)
      Returns a new instance of BackOffContext with the configured properties.
      Specified by:
      start in interface BackOffPolicy
      Parameters:
      context - the RetryContext context, which might contain information that we can use to decide how to proceed.
      Returns:
      the implementation-specific BackOffContext or 'null'.
    • backOff

      public void backOff(BackOffContext backOffContext) throws BackOffInterruptedException
      Pause for the current backoff interval.
      Specified by:
      backOff in interface BackOffPolicy
      Parameters:
      backOffContext - the BackOffContext
      Throws:
      BackOffInterruptedException - if the attempt at back off is interrupted.
    • toString

      public String toString()
      Overrides:
      toString in class Object