Interface RetryRule
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Determines whether a failed request should be retried.
If you need to determine by looking into the
Response
, use RetryRuleWithContent
.-
Method Summary
Modifier and TypeMethodDescriptionstatic RetryRuleBuilder
builder()
Returns a newly createdRetryRuleBuilder
.static RetryRuleBuilder
builder(HttpMethod... methods)
Returns a newly createdRetryRuleBuilder
with the specifiedHttpMethod
s.static RetryRuleBuilder
builder(Iterable<HttpMethod> methods)
Returns a newly createdRetryRuleBuilder
with the specifiedHttpMethod
s.static RetryRuleBuilder
builder(BiPredicate<? super ClientRequestContext,? super RequestHeaders> requestHeadersFilter)
Returns a newly createdRetryRuleBuilder
with the specifiedrequestHeadersFilter
.static RetryRule
failsafe()
Returns a newly createdRetryRule
that will retry with thedefault backoff
if the request HTTP method is idempotent and anException
is raised or the class of the response status isHttpStatusClass.SERVER_ERROR
.static RetryRule
Returns aRetryRule
that combines the specifiedretryRules
.static RetryRule
static RetryRule
static RetryRule
onException(Class<? extends Throwable> exception)
Returns a newly created aRetryRule
that will retry with the default backoff if anException
is raised and that is an instance of the specifiedexception
.static RetryRule
onException(BiPredicate<? super ClientRequestContext,? super Throwable> exceptionFilter)
Returns a newly createdRetryRule
that will retry with the default backoff if anException
is raised and the specifiedexceptionFilter
returnstrue
.static RetryRule
Returns a newly createdRetryRule
that will retry with the default backoff if the class of the response status isHttpStatusClass.SERVER_ERROR
.static RetryRule
onStatus(HttpStatus... statuses)
Returns a newly createdRetryRule
that will retry with the default backoff if the response status is one of the specifiedHttpStatus
es.static RetryRule
onStatus(Iterable<HttpStatus> statuses)
Returns a newly createdRetryRule
that will retry with the default backoff if the response status is one of the specifiedHttpStatus
es.static RetryRule
onStatus(BiPredicate<? super ClientRequestContext,? super HttpStatus> statusFilter)
Returns a newly created aRetryRule
that will retry with the default backoff if the response status matches the specifiedstatusFilter
.static RetryRule
onStatusClass(HttpStatusClass statusClass)
Returns a newly createdRetryRule
that will retry with the default backoff if the class of the response status is the specifiedHttpStatusClass
.static RetryRule
onStatusClass(Iterable<HttpStatusClass> statusClasses)
Returns a newly createdRetryRule
that will retry with the default backoff if the class of the response status is one of the specifiedHttpStatusClass
es.static RetryRule
Returns aRetryRule
that retries with the default backoff on anUnprocessedRequestException
which means that the request has not been processed by the server.default RetryRule
default boolean
Returns whether this rule requires the response trailers to determine if aResponse
is successful or not.shouldRetry(ClientRequestContext ctx, @Nullable Throwable cause)
Tells whether the request sent with the specifiedClientRequestContext
requires a retry or not.
-
Method Details
-
failsafe
Returns a newly createdRetryRule
that will retry with thedefault backoff
if the request HTTP method is idempotent and anException
is raised or the class of the response status isHttpStatusClass.SERVER_ERROR
. Otherwise, anUnprocessedRequestException
is raised regardless of the request HTTP method.Note that a client can safely retry a failed request with this rule if an endpoint service produces the same result (no side effects) on idempotent HTTP methods or
UnprocessedRequestException
.This method is a shortcut for:
RetryRule.of(RetryRule.builder(HttpMethods.idempotentMethods()) .onServerErrorStatus() .onUnprocessed() .thenBackoff(), RetryRule.onUnprocessed());
-
onStatusClass
Returns a newly createdRetryRule
that will retry with the default backoff if the class of the response status is the specifiedHttpStatusClass
. -
onStatusClass
Returns a newly createdRetryRule
that will retry with the default backoff if the class of the response status is one of the specifiedHttpStatusClass
es. -
onServerErrorStatus
Returns a newly createdRetryRule
that will retry with the default backoff if the class of the response status isHttpStatusClass.SERVER_ERROR
. -
onStatus
Returns a newly createdRetryRule
that will retry with the default backoff if the response status is one of the specifiedHttpStatus
es. -
onStatus
Returns a newly createdRetryRule
that will retry with the default backoff if the response status is one of the specifiedHttpStatus
es. -
onStatus
static RetryRule onStatus(BiPredicate<? super ClientRequestContext,? super HttpStatus> statusFilter)Returns a newly created aRetryRule
that will retry with the default backoff if the response status matches the specifiedstatusFilter
. -
onException
Returns a newly created aRetryRule
that will retry with the default backoff if anException
is raised and that is an instance of the specifiedexception
. -
onException
static RetryRule onException(BiPredicate<? super ClientRequestContext,? super Throwable> exceptionFilter)Returns a newly createdRetryRule
that will retry with the default backoff if anException
is raised and the specifiedexceptionFilter
returnstrue
. -
onException
Returns a newly createdRetryRule
that retries with default backoff on anyException
. Note that this rule should be used carefully because it reties regardless of idempotency. -
onUnprocessed
Returns aRetryRule
that retries with the default backoff on anUnprocessedRequestException
which means that the request has not been processed by the server. Therefore, you can safely retry the request without worrying about the idempotency of the request. -
builder
Returns a newly createdRetryRuleBuilder
. -
builder
Returns a newly createdRetryRuleBuilder
with the specifiedHttpMethod
s. -
builder
Returns a newly createdRetryRuleBuilder
with the specifiedHttpMethod
s. -
builder
static RetryRuleBuilder builder(BiPredicate<? super ClientRequestContext,? super RequestHeaders> requestHeadersFilter)Returns a newly createdRetryRuleBuilder
with the specifiedrequestHeadersFilter
. -
of
Returns aRetryRule
that combines the specifiedretryRules
. -
of
-
orElse
Returns a composedRetryRule
that represents a logical OR of thisRetryRule
and another. If thisRetryRule
completes withRetryDecision.next()
, then otherRetryRule
is evaluated. -
shouldRetry
CompletionStage<RetryDecision> shouldRetry(ClientRequestContext ctx, @Nullable @Nullable Throwable cause)Tells whether the request sent with the specifiedClientRequestContext
requires a retry or not. Implement this method to return aCompletionStage
and to complete it with a desiredRetryDecision.retry(Backoff)
. To not retry, complete it withRetryDecision.noRetry()
. To skip thisRetryRule
and find otherRetryRule
, complete it withRetryDecision.next()
. If the return value of the lastRetryRule
completes withRetryDecision.next()
, the request never retries.To retrieve the
ResponseHeaders
, you can use the specifiedClientRequestContext
:CompletionStage<RetryDecision> shouldRetry(ClientRequestContext ctx, @Nullable Throwable cause) { if (cause != null) { return CompletableFuture.completedFuture(RetryDecision.retry(backoff)); } ResponseHeaders responseHeaders = ctx.log().ensureAvailable(RequestLogProperty.RESPONSE_HEADERS) .responseHeaders(); if (responseHeaders.status().codeClass() == HttpStatusClass.SERVER_ERROR) { return CompletableFuture.completedFuture(RetryDecision.retry(backoff)); } if (responseHeaders.status() == HttpStatus.TOO_MANY_REQUESTS) { return CompletableFuture.completedFuture(RetryDecision.noRetry()); } return CompletableFuture.completedFuture(RetryDecision.next()); }
- Parameters:
ctx
- theClientRequestContext
of this requestcause
- theThrowable
which is raised while sending a request.null
if there's no exception.
-
requiresResponseTrailers
default boolean requiresResponseTrailers()Returns whether this rule requires the response trailers to determine if aResponse
is successful or not.
-