Delen via


RetryPolicy Class

  • java.lang.Object
    • com.azure.core.http.policy.RetryPolicy

Implements

public class RetryPolicy
implements HttpPipelinePolicy

The RetryPolicy class is an implementation of the HttpPipelinePolicy interface. This policy handles HTTP retries by determining if an HTTP request should be retried based on the received HttpResponse.

This class is useful when you need to handle HTTP retries in a pipeline. It uses a RetryStrategy to decide if a request should be retried. By default, it uses the ExponentialBackoff strategy, which uses a delay duration that exponentially increases with each retry attempt until an upper bound is reached.

Code sample:

In this example, a RetryPolicy is created which can then be added to the pipeline. For the request then sent by the pipeline, if the server responds with a status code that indicates a transient error, the request will be retried according to the RetryStrategy used by the RetryPolicy.

RetryPolicy retryPolicy = new RetryPolicy();

Constructor Summary

Constructor Description
RetryPolicy()

Creates RetryPolicy using ExponentialBackoff() as the RetryStrategy.

RetryPolicy(RetryOptions retryOptions)

Creates a RetryPolicy with the provided RetryOptions.

RetryPolicy(RetryStrategy retryStrategy)

Creates a RetryPolicy with the provided RetryStrategy.

RetryPolicy(RetryStrategy retryStrategy, String retryAfterHeader, ChronoUnit retryAfterTimeUnit)

Creates RetryPolicy with the provided RetryStrategy and default ExponentialBackoff as RetryStrategy.

RetryPolicy(String retryAfterHeader, ChronoUnit retryAfterTimeUnit)

Creates RetryPolicy using ExponentialBackoff() as the RetryStrategy and uses retryAfterHeader to look up the wait period in the returned HttpResponse to calculate the retry delay when a recoverable HTTP error is returned.

Method Summary

Modifier and Type Method and Description
Mono<HttpResponse> process(HttpPipelineCallContext context, HttpPipelineNextPolicy next)

Processes provided request context and invokes the next policy.

HttpResponse processSync(HttpPipelineCallContext context, HttpPipelineNextSyncPolicy next)

Processes provided request context and invokes the next policy synchronously.

Methods inherited from java.lang.Object

Constructor Details

RetryPolicy

public RetryPolicy()

Creates RetryPolicy using ExponentialBackoff() as the RetryStrategy.

RetryPolicy

public RetryPolicy(RetryOptions retryOptions)

Creates a RetryPolicy with the provided RetryOptions.

Parameters:

retryOptions - The RetryOptions used to configure this RetryPolicy.

RetryPolicy

public RetryPolicy(RetryStrategy retryStrategy)

Creates a RetryPolicy with the provided RetryStrategy.

Parameters:

retryStrategy - The RetryStrategy used for retries.

RetryPolicy

public RetryPolicy(RetryStrategy retryStrategy, String retryAfterHeader, ChronoUnit retryAfterTimeUnit)

Creates RetryPolicy with the provided RetryStrategy and default ExponentialBackoff as RetryStrategy. It will use provided retryAfterHeader in HttpResponse headers for calculating retry delay.

Parameters:

retryStrategy - The RetryStrategy used for retries.
retryAfterHeader - The HTTP header, such as 'Retry-After' or 'x-ms-retry-after-ms', to lookup for the retry delay. If the value is null, RetryPolicy will use the retry strategy to compute the delay and ignore the delay provided in response header.
retryAfterTimeUnit - The time unit to use when applying the retry delay. null is valid if, and only if, retryAfterHeader is null.

RetryPolicy

public RetryPolicy(String retryAfterHeader, ChronoUnit retryAfterTimeUnit)

Creates RetryPolicy using ExponentialBackoff() as the RetryStrategy and uses retryAfterHeader to look up the wait period in the returned HttpResponse to calculate the retry delay when a recoverable HTTP error is returned.

Parameters:

retryAfterHeader - The HTTP header, such as Retry-After or x-ms-retry-after-ms, to lookup for the retry delay. If the value is null, calculateRetryDelay(int retryAttempts) will compute the delay and ignore the delay provided in response header.
retryAfterTimeUnit - The time unit to use when applying the retry delay. Null is valid if, and only if, retryAfterHeader is null.

Method Details

process

public Mono process(HttpPipelineCallContext context, HttpPipelineNextPolicy next)

Processes provided request context and invokes the next policy.

Parameters:

context
next

processSync

public HttpResponse processSync(HttpPipelineCallContext context, HttpPipelineNextSyncPolicy next)

Processes provided request context and invokes the next policy synchronously.

Parameters:

context
next

Applies to