Class ExponentialBackoff
- All Implemented Interfaces:
RetryStrategy
The ExponentialBackoff class is an implementation of the RetryStrategy interface. This strategy uses
a delay duration that exponentially increases with each retry attempt until an upper bound is reached, after which
every retry attempt is delayed by the provided max delay duration.
This class is useful when you need to handle retries for operations that may transiently fail. It ensures that the retries are performed with an increasing delay to avoid overloading the system.
Code sample:
In this example, an ExponentialBackoff is created and used in a RetryPolicy which can be added to
a pipeline. For a request sent by the pipeline, if the server responds with a transient error, the request will be
retried with an exponentially increasing delay.
ExponentialBackoff retryStrategy = new ExponentialBackoff(); RetryPolicy policy = new RetryPolicy(retryStrategy);
- See Also:
-
Field Summary
Fields inherited from interface com.azure.core.http.policy.RetryStrategy
HTTP_STATUS_TOO_MANY_REQUESTS -
Constructor Summary
ConstructorsConstructorDescriptionCreates an instance ofExponentialBackoffwith a maximum number of retry attempts configured by the environment propertyConfiguration.PROPERTY_AZURE_REQUEST_RETRY_COUNT, or three if it isn't configured or is less than or equal to 0.ExponentialBackoff(int maxRetries, Duration baseDelay, Duration maxDelay) Creates an instance ofExponentialBackoff.Creates an instance ofExponentialBackoff. -
Method Summary
Modifier and TypeMethodDescriptioncalculateRetryDelay(int retryAttempts) Computes the delay between each retry.intMax number of retry attempts to be made.booleanshouldRetryCondition(RequestRetryCondition requestRetryCondition) This method is consulted to determine if a retry attempt should be made for the givenRequestRetryCondition.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.azure.core.http.policy.RetryStrategy
calculateRetryDelay, shouldRetry, shouldRetryException
-
Constructor Details
-
ExponentialBackoff
public ExponentialBackoff()Creates an instance ofExponentialBackoffwith a maximum number of retry attempts configured by the environment propertyConfiguration.PROPERTY_AZURE_REQUEST_RETRY_COUNT, or three if it isn't configured or is less than or equal to 0. This strategy starts with a delay of 800 milliseconds and exponentially increases with each additional retry attempt to a maximum of 8 seconds. -
ExponentialBackoff
Creates an instance ofExponentialBackoff.- Parameters:
options- TheExponentialBackoffOptions.- Throws:
NullPointerException- ifoptionsisnull.
-
ExponentialBackoff
Creates an instance ofExponentialBackoff.- Parameters:
maxRetries- The max retry attempts that can be made.baseDelay- The base delay duration for retry.maxDelay- The max delay duration for retry.- Throws:
IllegalArgumentException- ifmaxRetriesis less than 0 orbaseDelayis less than or equal to 0 ormaxDelayis less thanbaseDelay.
-
-
Method Details
-
getMaxRetries
public int getMaxRetries()Description copied from interface:RetryStrategyMax number of retry attempts to be made.- Specified by:
getMaxRetriesin interfaceRetryStrategy- Returns:
- The max number of retry attempts.
-
calculateRetryDelay
Description copied from interface:RetryStrategyComputes the delay between each retry.If both this method and
RetryStrategy.calculateRetryDelay(RequestRetryCondition)are overridden, this method is ignored.- Specified by:
calculateRetryDelayin interfaceRetryStrategy- Parameters:
retryAttempts- The number of retry attempts completed so far.- Returns:
- The delay duration before the next retry.
-
shouldRetryCondition
Description copied from interface:RetryStrategyThis method is consulted to determine if a retry attempt should be made for the givenRequestRetryCondition.By default, if the
RequestRetryConditioncontains a non-nullHttpResponse, then theRetryStrategy.shouldRetry(HttpResponse)method is called, otherwise theRetryStrategy.shouldRetryException(Throwable)method is called.- Specified by:
shouldRetryConditionin interfaceRetryStrategy- Parameters:
requestRetryCondition- TheRequestRetryConditioncontaining information that can be used to determine if the request should be retried.- Returns:
- Whether a retry should be attempted.
-