Package org.springframework.util.backoff

# Class ExponentialBackOff

java.lang.Object
org.springframework.util.backoff.ExponentialBackOff

All Implemented Interfaces:
  `BackOff`

public class ExponentialBackOff extends Object implements BackOff
Implementation of `BackOff` that increases the back off period for each retry attempt. When the interval has reached the max interval, it is no longer increased. Stops retrying once the max elapsed time has been reached.

Example: The default interval is 2000L ms; the default multiplier is 1.5; and the default max interval is 30000L. For 10 attempts the sequence will be as follows:

     request#     back off

      1              2000
      2              3000
      3              4500
      4              6750
      5             10125
      6             15187
      7             22780
      8             30000
      9             30000
     10             30000

Note that the default max elapsed time is `Long.MAX_VALUE`, and the default maximum number of attempts is `Integer.MAX_VALUE`. Use `setMaxElapsedTime(long)` to limit the length of time that an instance should accumulate before returning `BackOffExecution.STOP`. Alternatively, use `setMaxAttempts(int)` to limit the number of attempts. The execution stops when either of those two limits is reached.

Since:
  4.1
Author:
  Stephane Nicoll, Gary Russell

## Field Summary

Fields
Modifier and Type
Field
Description
`static final long`
`DEFAULT_INITIAL_INTERVAL`
The default initial interval.
`static final int`
`DEFAULT_MAX_ATTEMPTS`
The default maximum attempts.
`static final long`
`DEFAULT_MAX_ELAPSED_TIME`
The default maximum elapsed time.
`static final long`
`DEFAULT_MAX_INTERVAL`
The default maximum back off time.
`static final double`
`DEFAULT_MULTIPLIER`
The default multiplier (increases the interval by 50%).

## Constructor Summary

Constructors
Constructor
Description
`ExponentialBackOff()`
Create an instance with the default settings.
`ExponentialBackOff(long initialInterval, double multiplier)`
Create an instance with the supplied settings.

## Method Summary

Modifier and Type
Method
Description
`long`
`getInitialInterval()`
Return the initial interval in milliseconds.
`int`
`getMaxAttempts()`
Return the maximum number of attempts after which a call to `BackOffExecution.nextBackOff()` returns `BackOffExecution.STOP`.
`long`
`getMaxElapsedTime()`
Return the maximum elapsed time in milliseconds after which a call to `BackOffExecution.nextBackOff()` returns `BackOffExecution.STOP`.
`long`
`getMaxInterval()`
Return the maximum back off time in milliseconds.
`double`
`getMultiplier()`
Return the value to multiply the current interval by for each retry attempt.
`void`
`setInitialInterval(long initialInterval)`
Set the initial interval in milliseconds.
`void`
`setMaxAttempts(int maxAttempts)`
The maximum number of attempts after which a call to `BackOffExecution.nextBackOff()` returns `BackOffExecution.STOP`.
`void`
`setMaxElapsedTime(long maxElapsedTime)`
Set the maximum elapsed time in milliseconds after which a call to `BackOffExecution.nextBackOff()` returns `BackOffExecution.STOP`.
`void`
`setMaxInterval(long maxInterval)`
Set the maximum back off time in milliseconds.
`void`
`setMultiplier(double multiplier)`
Set the value to multiply the current interval by for each retry attempt.
`BackOffExecution`
`start()`
Start a new back off execution.
`String`
`toString()`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

## Field Details

### DEFAULT_INITIAL_INTERVAL

public static final long DEFAULT_INITIAL_INTERVAL
The default initial interval.

See Also:
  - Constant Field Values

### DEFAULT_MULTIPLIER

public static final double DEFAULT_MULTIPLIER
The default multiplier (increases the interval by 50%).

See Also:
  - Constant Field Values

### DEFAULT_MAX_INTERVAL

public static final long DEFAULT_MAX_INTERVAL
The default maximum back off time.

See Also:
  - Constant Field Values

### DEFAULT_MAX_ELAPSED_TIME

public static final long DEFAULT_MAX_ELAPSED_TIME
The default maximum elapsed time.

See Also:
  - Constant Field Values

### DEFAULT_MAX_ATTEMPTS

public static final int DEFAULT_MAX_ATTEMPTS
The default maximum attempts.

Since:
  6.1
See Also:
  - Constant Field Values

## Constructor Details

### ExponentialBackOff

public ExponentialBackOff()
Create an instance with the default settings.

See Also:
  - `DEFAULT_INITIAL_INTERVAL`
  - `DEFAULT_MULTIPLIER`
  - `DEFAULT_MAX_INTERVAL`
  - `DEFAULT_MAX_ELAPSED_TIME`
  - `DEFAULT_MAX_ATTEMPTS`

### ExponentialBackOff

public ExponentialBackOff(long initialInterval, double multiplier)
Create an instance with the supplied settings.

Parameters:
  `initialInterval` - the initial interval in milliseconds
  `multiplier` - the multiplier (should be greater than or equal to 1)

## Method Details

### setInitialInterval

public void setInitialInterval(long initialInterval)
Set the initial interval in milliseconds.

### getInitialInterval

public long getInitialInterval()
Return the initial interval in milliseconds.

### setMultiplier

public void setMultiplier(double multiplier)
Set the value to multiply the current interval by for each retry attempt.

### getMultiplier

public double getMultiplier()
Return the value to multiply the current interval by for each retry attempt.

### setMaxInterval

public void setMaxInterval(long maxInterval)
Set the maximum back off time in milliseconds.

### getMaxInterval

public long getMaxInterval()
Return the maximum back off time in milliseconds.

### setMaxElapsedTime

public void setMaxElapsedTime(long maxElapsedTime)
Set the maximum elapsed time in milliseconds after which a call to `BackOffExecution.nextBackOff()` returns `BackOffExecution.STOP`.

Parameters:
  `maxElapsedTime` - the maximum elapsed time
See Also:
  - `setMaxAttempts(int)`

### getMaxElapsedTime

public long getMaxElapsedTime()
Return the maximum elapsed time in milliseconds after which a call to `BackOffExecution.nextBackOff()` returns `BackOffExecution.STOP`.

Returns:
  the maximum elapsed time
See Also:
  - `getMaxAttempts()`

### setMaxAttempts

public void setMaxAttempts(int maxAttempts)
The maximum number of attempts after which a call to `BackOffExecution.nextBackOff()` returns `BackOffExecution.STOP`.

Parameters:
  `maxAttempts` - the maximum number of attempts
Since:
  6.1
See Also:
  - `setMaxElapsedTime(long)`

### getMaxAttempts

public int getMaxAttempts()
Return the maximum number of attempts after which a call to `BackOffExecution.nextBackOff()` returns `BackOffExecution.STOP`.

Returns:
  the maximum number of attempts
Since:
  6.1
See Also:
  - `getMaxElapsedTime()`

### start

public BackOffExecution start()
Description copied from interface: `BackOff`
Start a new back off execution.

Specified by:
  `start` in interface `BackOff`
Returns:
  a fresh `BackOffExecution` ready to be used

### toString

public String toString()

Overrides:
  `toString` in class `Object`