Class ExponentialBackoffPolicy

  • All Implemented Interfaces:
    RetryBackoffPolicy

    public class ExponentialBackoffPolicy
    extends Object
    implements RetryBackoffPolicy
    A backoff policy that waits exponentially longer between each attempt (but keeps a constant delay once a maximum delay is reached).

    This policy uses the following formula to calculate the backoff delay:

    
       (2 ^ (attempt -1)) * baseDelay
     

    Note: for the very first attempt the answer is always 0 (i.e. retry immediately).

    Parameters of this policy are:

    • BaseDelay - multiplier for each attempt
    • MaxDelay - maximum delay (calculated delay will never exceed this value)

    Examples of calculations:

    
     BaseDelay: 10  MaxDelay: 3000
     -------------------------------
      Attempt 0  -> 0
      Attempt 1  -> 10
      Attempt 2  -> 20
      Attempt 3  -> 40
      Attempt 4  -> 80
      Attempt 5  -> 160
      Attempt 6  -> 320
      Attempt 7  -> 640
      Attempt 8  -> 1280
      Attempt 9  -> 2560
      Attempt 10 -> 3000
      ...        -> 3000
     
    
     BaseDelay: 500  MaxDelay: 30000
     -------------------------------
      Attempt 0 -> 0
      Attempt 1 -> 500
      Attempt 2 -> 1000
      Attempt 3 -> 2000
      Attempt 4 -> 4000
      Attempt 5 -> 8000
      Attempt 6 -> 16000
      Attempt 7 -> 30000
      ...       -> 30000
     
    • Field Detail

      • DEFAULT_BASE_DELAY

        public static final long DEFAULT_BASE_DELAY
        Default base delay (=50L) in milliseconds.
        See Also:
        Constant Field Values
      • DEFAULT_MAX_DELAY

        public static final long DEFAULT_MAX_DELAY
        Default maximum delay (=3000L) in milliseconds.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ExponentialBackoffPolicy

        public ExponentialBackoffPolicy​(long baseDelay,
                                        long maxDelay)
        Constructs a new instance.
        Parameters:
        baseDelay - Multiplier for each attempt (in milliseconds).
        maxDelay - Maximum delay in milliseconds (calculated delay will never exceed this value).
    • Method Detail

      • baseDelay

        public long baseDelay()
        Returns the base delay of this policy in milliseconds.
        Returns:
        Base delay in milliseconds.
      • maxDelay

        public long maxDelay()
        Returns the maximum delay of this policy in milliseconds.
        Returns:
        Maximum delay in milliseconds.
      • delayBeforeRetry

        public long delayBeforeRetry​(int attempt)
        Description copied from interface: RetryBackoffPolicy
        Returns a delay in milliseconds before the next retry attempt.
        Specified by:
        delayBeforeRetry in interface RetryBackoffPolicy
        Parameters:
        attempt - Attempt (starting with zero for the very first attempt).
        Returns:
        Delay in milliseconds.