5

Currently I'm replacing existing org.apache.http.* http client library with JDK-11's new Http library. There are many exciting new features, however I've not found anything on "how to set RetryRequestHandler in new HTTPClient". Code snippet of previous Apache HttpClient builder:

    ...
    ...
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
            .setDefaultRequestConfig(config)
            .setConnectionManager(connectionManager)
    if(retryCount > 0) {
        httpClientBuilder.setRetryHandler(new RetryRequestHandler(retryCount, url));
    }
    if (proxyHost) {
        HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http");
        httpClientBuilder.setProxy(proxy);
    }
    ...
    ...

Here RetryRequestHandler is an extension of DefaultHttpRequestRetryHandler

public class RetryRequestHandler extends DefaultHttpRequestRetryHandler{...} 

There is no option to set retry in java-11's new HttpClient. Is there any workaround to do so?

MD Ruhul Amin
  • 4,386
  • 1
  • 22
  • 37
  • I doubt there is any *retry* feature in the default JDK implementation. – Naman Feb 13 '19 at 04:58
  • Your code does not look like Java 11 http client. Instead it seems to use Apache HttpClient: [DefaultHttpRequestRetryHandler](https://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/DefaultHttpRequestRetryHandler.html) – Robert Feb 14 '19 at 18:16
  • @robert Code snippet of previous httpclient builder – MD Ruhul Amin Feb 14 '19 at 18:40
  • 1
    @ruhul What solution did you end up implementing? – mttdbrd Mar 22 '19 at 19:56

2 Answers2

1

Spring has broken retry out of Spring Batch into a separate, standalone library that you can use (albeit inside a Spring project). It will allow you to add a retry policy to a method calling the new HTTP client. See docs below:

https://github.com/spring-projects/spring-retry

That's the closest thing I know for this situation. If you want to roll your own, you could also accomplish the same thing with aspects. I think the Spring library is cleaner because you can let the library handle the details of retry. They also have a powerful set of APIs for different retry policies, including exponential backoff, etc.

mttdbrd
  • 1,791
  • 12
  • 17
0

The java.net.http HttpClient will retry idempotent requests (GET/HEAD) once by default. This is typically useful on HTTP/1.1 long live connections where the server side might arbitrarily decide that the connection has remained idle for too long, and closes it at the same time that the client takes it out of the pool and starts sending a new request.

daniel
  • 2,665
  • 1
  • 8
  • 18
  • Thanks for the info. But I'm looking for some implementation where I can attach a retry handler and it will automatically retry a fixed number of time, in case of time out or particular exception. – MD Ruhul Amin Feb 13 '19 at 11:15
  • You could easily do this by registering an asynchronous dependent action to the `CompletableFuture` returned by `HttpClient::sendAsync`. – daniel Feb 13 '19 at 11:17