Module methanol

Class RetryInterceptor

  • All Implemented Interfaces:
    Methanol.Interceptor

    public final class RetryInterceptor
    extends java.lang.Object
    implements Methanol.Interceptor
    An interceptor that retries HTTP requests based on configurable conditions.

    Retry conditions are evaluated in the order they are added. The first matching condition determines the next request to send. Later conditions are not evaluated.

    Example:

    Retry server errors with exponential backoff:
    
     var client = Methanol.newBuilder()
         .interceptor(RetryInterceptor.newBuilder()
             .maxRetries(3)
             .backoff(BackoffStrategy.exponential(
                 Duration.ofMillis(100),
                 Duration.ofSeconds(10)).withJitter())
             .onStatus(500, 502, 503, 504)
             .onException(ConnectException.class)
             .build())
         .build();
     
    See Also:
    RetryInterceptor.Builder
    • Method Detail

      • intercept

        public <T> java.net.http.HttpResponse<T> intercept​(java.net.http.HttpRequest request,
                                                           Methanol.Interceptor.Chain<T> chain)
                                                    throws java.io.IOException,
                                                           java.lang.InterruptedException
        Description copied from interface: Methanol.Interceptor
        Intercepts given request and returns the resulting response, usually by forwarding to the given chain.
        Specified by:
        intercept in interface Methanol.Interceptor
        Throws:
        java.io.IOException
        java.lang.InterruptedException
      • interceptAsync

        public <T> java.util.concurrent.CompletableFuture<java.net.http.HttpResponse<T>> interceptAsync​(java.net.http.HttpRequest request,
                                                                                                        Methanol.Interceptor.Chain<T> chain)
        Description copied from interface: Methanol.Interceptor
        Intercepts the given request and returns a CompletableFuture for the resulting response, usually by forwarding to the given chain.
        Specified by:
        interceptAsync in interface Methanol.Interceptor