Class RetryInterceptor.Builder
java.lang.Object
com.github.mizosoft.methanol.RetryInterceptor.Builder
- Enclosing class:
RetryInterceptor
A builder of
RetryInterceptor instances.
Note: Retry conditions are evaluated in the order they are added. The first matching condition determines the next request. Later conditions are not evaluated.
-
Method Summary
Modifier and TypeMethodDescriptionbackoff(RetryInterceptor.BackoffStrategy backoffStrategy) Specifies theBackoffStrategyto apply every retry.beginWith(Function<HttpRequest, HttpRequest> requestModifier) Specifies the request modifier to be applied before the request is sent for the first time (attempt 0, not a retry).build()Builds a newRetryInterceptorthat retries all requests based on the conditions specified so far.build(BiPredicate<HttpRequest, Methanol.Interceptor.Chain<?>> selector) Builds a newRetryInterceptorthat only retries requests matched by the given predicate based on the conditions specified so far.build(Predicate<HttpRequest> selector) Builds a newRetryInterceptorthat only retries requests matched by the given predicate based on the conditions specified so far.listener(RetryInterceptor.Listener listener) Sets the listener for retry events.maxRetries(int maxRetries) Specifies the maximum times the interceptor is allowed to retry the request.on(Predicate<RetryInterceptor.Context<?>> predicate) Specifies that the interceptor is to retry the request if the retryRetryInterceptor.Contextsatisfies the given predicate.on(Predicate<RetryInterceptor.Context<?>> predicate, Function<RetryInterceptor.Context<?>, HttpRequest> requestModifier) Specifies that the interceptor is to retry the request, after applying the given request modifier, if the retryRetryInterceptor.Contextsatisfies the given predicate.final RetryInterceptor.BuilderonException(Class<? extends Throwable>... exceptionTypes) Specifies that the interceptor is to retry the request if an exception with any of the given types is thrown.onException(Predicate<Throwable> exceptionPredicate) Specifies that the interceptor is to retry the request if an exception that satisfies the given predicate is thrown.onException(Predicate<Throwable> exceptionPredicate, Function<RetryInterceptor.Context<?>, HttpRequest> requestModifier) Specifies that the interceptor is to retry the request, after applying the given request modifier, if an exception that satisfies the given predicate is thrown.onException(Set<Class<? extends Throwable>> exceptionTypes, Function<RetryInterceptor.Context<?>, HttpRequest> requestModifier) Specifies that the interceptor is to retry the request, after applying the given request modifier, if an exception with any of the given types is thrown.onResponse(Predicate<HttpResponse<?>> responsePredicate) Specifies that the interceptor is to retry the request if a response that satisfies the given predicate is received.onResponse(Predicate<HttpResponse<?>> responsePredicate, Function<RetryInterceptor.Context<?>, HttpRequest> requestModifier) Specifies that the interceptor is to retry the request, after applying the given request modifier, if a response that satisfies the given predicate is received.onStatus(int... codes) Specifies that the interceptor is to retry the request if a response with any of the given status codes is received.Specifies that the interceptor is to retry the request if a response whose code satisfies the given predicate is received.onStatus(Predicate<Integer> statusPredicate, Function<RetryInterceptor.Context<?>, HttpRequest> requestModifier) Specifies that the interceptor is to retry the request, after applying the given request modifier, if a response whose code satisfies the given predicate is received.onStatus(Set<Integer> codes, Function<RetryInterceptor.Context<?>, HttpRequest> requestModifier) Specifies that the interceptor is to retry the request, after applying the given request modifier, if a response with any of the given status codes is received.Specifies that requests that are not idempotent are to be retried.Specifies that the interceptor is to throw anHttpRetriesExhaustedExceptionif the maximum number of retries is exceeded, with the last-thrown exception (if any) added as a suppressed exception.Sets a timeout for the entire retry process.
-
Method Details
-
beginWith
@CanIgnoreReturnValue public RetryInterceptor.Builder beginWith(Function<HttpRequest, HttpRequest> requestModifier) Specifies the request modifier to be applied before the request is sent for the first time (attempt 0, not a retry). This can be used to add request headers that should be present on all attempts. -
maxRetries
Specifies the maximum times the interceptor is allowed to retry the request. After the given number of retries is exhausted, the interceptor returns the request or exception as-is, or throws anHttpRetriesExhaustedExceptionifthrowOnExhaustion()is specified. The defaultmaxRetriesis 5. -
backoff
@CanIgnoreReturnValue public RetryInterceptor.Builder backoff(RetryInterceptor.BackoffStrategy backoffStrategy) Specifies theBackoffStrategyto apply every retry. The defaultBackoffStrategyisRetryInterceptor.BackoffStrategy.none(). -
onException
@SafeVarargs @CanIgnoreReturnValue public final RetryInterceptor.Builder onException(Class<? extends Throwable>... exceptionTypes) Specifies that the interceptor is to retry the request if an exception with any of the given types is thrown. -
onException
@CanIgnoreReturnValue public RetryInterceptor.Builder onException(Set<Class<? extends Throwable>> exceptionTypes, Function<RetryInterceptor.Context<?>, HttpRequest> requestModifier) Specifies that the interceptor is to retry the request, after applying the given request modifier, if an exception with any of the given types is thrown. -
onException
@CanIgnoreReturnValue public RetryInterceptor.Builder onException(Predicate<Throwable> exceptionPredicate) Specifies that the interceptor is to retry the request if an exception that satisfies the given predicate is thrown. -
onException
@CanIgnoreReturnValue public RetryInterceptor.Builder onException(Predicate<Throwable> exceptionPredicate, Function<RetryInterceptor.Context<?>, HttpRequest> requestModifier) Specifies that the interceptor is to retry the request, after applying the given request modifier, if an exception that satisfies the given predicate is thrown. -
onStatus
Specifies that the interceptor is to retry the request if a response with any of the given status codes is received. -
onStatus
@CanIgnoreReturnValue public RetryInterceptor.Builder onStatus(Set<Integer> codes, Function<RetryInterceptor.Context<?>, HttpRequest> requestModifier) Specifies that the interceptor is to retry the request, after applying the given request modifier, if a response with any of the given status codes is received. -
onStatus
Specifies that the interceptor is to retry the request if a response whose code satisfies the given predicate is received. -
onStatus
@CanIgnoreReturnValue public RetryInterceptor.Builder onStatus(Predicate<Integer> statusPredicate, Function<RetryInterceptor.Context<?>, HttpRequest> requestModifier) Specifies that the interceptor is to retry the request, after applying the given request modifier, if a response whose code satisfies the given predicate is received. -
onResponse
@CanIgnoreReturnValue public RetryInterceptor.Builder onResponse(Predicate<HttpResponse<?>> responsePredicate) Specifies that the interceptor is to retry the request if a response that satisfies the given predicate is received. -
onResponse
@CanIgnoreReturnValue public RetryInterceptor.Builder onResponse(Predicate<HttpResponse<?>> responsePredicate, Function<RetryInterceptor.Context<?>, HttpRequest> requestModifier) Specifies that the interceptor is to retry the request, after applying the given request modifier, if a response that satisfies the given predicate is received. -
on
@CanIgnoreReturnValue public RetryInterceptor.Builder on(Predicate<RetryInterceptor.Context<?>> predicate) Specifies that the interceptor is to retry the request if the retryRetryInterceptor.Contextsatisfies the given predicate. -
on
@CanIgnoreReturnValue public RetryInterceptor.Builder on(Predicate<RetryInterceptor.Context<?>> predicate, Function<RetryInterceptor.Context<?>, HttpRequest> requestModifier) Specifies that the interceptor is to retry the request, after applying the given request modifier, if the retryRetryInterceptor.Contextsatisfies the given predicate. -
timeout
Sets a timeout for the entire retry process. If the timeout is exceeded before receiving a non-retryable response or exception, anHttpTimeoutExceptionis thrown with the last exception (if any) as a suppressed exception.The timeout applies from the start of the first attempt until either:
- A non-retryable response or exception is received
- Maximum retries are exhausted
- The timeout expires
-
listener
Sets the listener for retry events. -
throwOnExhaustion
Specifies that the interceptor is to throw anHttpRetriesExhaustedExceptionif the maximum number of retries is exceeded, with the last-thrown exception (if any) added as a suppressed exception.By default, when retries are exhausted, the last response or exception is returned/thrown as-is. This allows you to specify a "give up and throw" behavior on the interceptor.
-
retryNonIdempotent
Specifies that requests that are not idempotent are to be retried. By default, only idempotent requests are retried. -
build
Builds a newRetryInterceptorthat retries all requests based on the conditions specified so far. -
build
Builds a newRetryInterceptorthat only retries requests matched by the given predicate based on the conditions specified so far. -
build
Builds a newRetryInterceptorthat only retries requests matched by the given predicate based on the conditions specified so far.
-