Module methanol

Class RetryInterceptor.Builder

  • Enclosing class:
    RetryInterceptor

    public static final class RetryInterceptor.Builder
    extends java.lang.Object
    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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      RetryInterceptor.Builder backoff​(RetryInterceptor.BackoffStrategy backoffStrategy)
      Specifies the BackoffStrategy to apply every retry.
      RetryInterceptor.Builder beginWith​(java.util.function.Function<java.net.http.HttpRequest,​java.net.http.HttpRequest> requestModifier)
      Specifies the request modifier to be applied before the request is sent for the first time (attempt 0, not a retry).
      RetryInterceptor build()
      Builds a new RetryInterceptor that retries all requests based on the conditions specified so far.
      RetryInterceptor build​(java.util.function.BiPredicate<java.net.http.HttpRequest,​Methanol.Interceptor.Chain<?>> selector)
      Builds a new RetryInterceptor that only retries requests matched by the given predicate based on the conditions specified so far.
      RetryInterceptor build​(java.util.function.Predicate<java.net.http.HttpRequest> selector)
      Builds a new RetryInterceptor that only retries requests matched by the given predicate based on the conditions specified so far.
      RetryInterceptor.Builder listener​(RetryInterceptor.Listener listener)
      Sets the listener for retry events.
      RetryInterceptor.Builder maxRetries​(int maxRetries)
      Specifies the maximum times the interceptor is allowed to retry the request.
      RetryInterceptor.Builder on​(java.util.function.Predicate<RetryInterceptor.Context<?>> predicate)
      Specifies that the interceptor is to retry the request if the retry RetryInterceptor.Context satisfies the given predicate.
      RetryInterceptor.Builder on​(java.util.function.Predicate<RetryInterceptor.Context<?>> predicate, java.util.function.Function<RetryInterceptor.Context<?>,​java.net.http.HttpRequest> requestModifier)
      Specifies that the interceptor is to retry the request, after applying the given request modifier, if the retry RetryInterceptor.Context satisfies the given predicate.
      RetryInterceptor.Builder onException​(java.lang.Class<? extends java.lang.Throwable>... exceptionTypes)
      Specifies that the interceptor is to retry the request if an exception with any of the given types is thrown.
      RetryInterceptor.Builder onException​(java.util.function.Predicate<java.lang.Throwable> exceptionPredicate)
      Specifies that the interceptor is to retry the request if an exception that satisfies the given predicate is thrown.
      RetryInterceptor.Builder onException​(java.util.function.Predicate<java.lang.Throwable> exceptionPredicate, java.util.function.Function<RetryInterceptor.Context<?>,​java.net.http.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.
      RetryInterceptor.Builder onException​(java.util.Set<java.lang.Class<? extends java.lang.Throwable>> exceptionTypes, java.util.function.Function<RetryInterceptor.Context<?>,​java.net.http.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.
      RetryInterceptor.Builder onResponse​(java.util.function.Predicate<java.net.http.HttpResponse<?>> responsePredicate)
      Specifies that the interceptor is to retry the request if a response that satisfies the given predicate is received.
      RetryInterceptor.Builder onResponse​(java.util.function.Predicate<java.net.http.HttpResponse<?>> responsePredicate, java.util.function.Function<RetryInterceptor.Context<?>,​java.net.http.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.
      RetryInterceptor.Builder onStatus​(int... codes)
      Specifies that the interceptor is to retry the request if a response with any of the given status codes is received.
      RetryInterceptor.Builder onStatus​(java.util.function.Predicate<java.lang.Integer> statusPredicate)
      Specifies that the interceptor is to retry the request if a response whose code satisfies the given predicate is received.
      RetryInterceptor.Builder onStatus​(java.util.function.Predicate<java.lang.Integer> statusPredicate, java.util.function.Function<RetryInterceptor.Context<?>,​java.net.http.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.
      RetryInterceptor.Builder onStatus​(java.util.Set<java.lang.Integer> codes, java.util.function.Function<RetryInterceptor.Context<?>,​java.net.http.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.
      RetryInterceptor.Builder retryNonIdempotent()
      Specifies that requests that are not idempotent are to be retried.
      RetryInterceptor.Builder throwOnExhaustion()
      Specifies that the interceptor is to throw an HttpRetriesExhaustedException if the maximum number of retries is exceeded, with the last-thrown exception (if any) added as a suppressed exception.
      RetryInterceptor.Builder timeout​(java.time.Duration timeout)
      Sets a timeout for the entire retry process.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • beginWith

        @CanIgnoreReturnValue
        public RetryInterceptor.Builder beginWith​(java.util.function.Function<java.net.http.HttpRequest,​java.net.http.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

        @CanIgnoreReturnValue
        public RetryInterceptor.Builder maxRetries​(int 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 an HttpRetriesExhaustedException if throwOnExhaustion() is specified. The default maxRetries is 5.
      • onException

        @SafeVarargs
        @CanIgnoreReturnValue
        public final RetryInterceptor.Builder onException​(java.lang.Class<? extends java.lang.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​(java.util.Set<java.lang.Class<? extends java.lang.Throwable>> exceptionTypes,
                                                    java.util.function.Function<RetryInterceptor.Context<?>,​java.net.http.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​(java.util.function.Predicate<java.lang.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​(java.util.function.Predicate<java.lang.Throwable> exceptionPredicate,
                                                    java.util.function.Function<RetryInterceptor.Context<?>,​java.net.http.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

        @CanIgnoreReturnValue
        public RetryInterceptor.Builder onStatus​(int... codes)
        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​(java.util.Set<java.lang.Integer> codes,
                                                 java.util.function.Function<RetryInterceptor.Context<?>,​java.net.http.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

        @CanIgnoreReturnValue
        public RetryInterceptor.Builder onStatus​(java.util.function.Predicate<java.lang.Integer> statusPredicate)
        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​(java.util.function.Predicate<java.lang.Integer> statusPredicate,
                                                 java.util.function.Function<RetryInterceptor.Context<?>,​java.net.http.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​(java.util.function.Predicate<java.net.http.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​(java.util.function.Predicate<java.net.http.HttpResponse<?>> responsePredicate,
                                                   java.util.function.Function<RetryInterceptor.Context<?>,​java.net.http.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.
      • timeout

        @CanIgnoreReturnValue
        public RetryInterceptor.Builder timeout​(java.time.Duration timeout)
        Sets a timeout for the entire retry process. If the timeout is exceeded before receiving a non-retryable response or exception, an HttpTimeoutException is 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
      • throwOnExhaustion

        @CanIgnoreReturnValue
        public RetryInterceptor.Builder throwOnExhaustion()
        Specifies that the interceptor is to throw an HttpRetriesExhaustedException if 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

        @CanIgnoreReturnValue
        public RetryInterceptor.Builder retryNonIdempotent()
        Specifies that requests that are not idempotent are to be retried. By default, only idempotent requests are retried.
      • build

        public RetryInterceptor build()
        Builds a new RetryInterceptor that retries all requests based on the conditions specified so far.
      • build

        public RetryInterceptor build​(java.util.function.Predicate<java.net.http.HttpRequest> selector)
        Builds a new RetryInterceptor that only retries requests matched by the given predicate based on the conditions specified so far.
      • build

        public RetryInterceptor build​(java.util.function.BiPredicate<java.net.http.HttpRequest,​Methanol.Interceptor.Chain<?>> selector)
        Builds a new RetryInterceptor that only retries requests matched by the given predicate based on the conditions specified so far.