Migrate to Apache HttpClient 5.x

627 Views Asked by At

How can I migrate to setRetryStrategy for below Apache httpcomponents-client 5.x

HttpClientBuilder cb = HttpClients.custom();
cb.setRetryHandler(new DefaultHttpRequestRetryHandler(X, true));

There is no parameter of requestSentRetryEnabled in latest DefaultHttpRequestStrategy

Migration to httpcomponents-client 5.x

1

There are 1 best solutions below

0
ozkanpakdil On

requestSentRetryEnabled is not required, if you setRetryHandler to DefaultHttpRequestStrategy which will retry if the error is 503 example code below

public static void main(String[] args) throws IOException {
    final ClassicHttpRequest httpGet = ClassicRequestBuilder.get("https://httpbin.org/status/503")
            .build();

    CloseableHttpClient httpClient = HttpClientBuilder
            .create()
            .setRetryStrategy(new DefaultHttpRequestRetryStrategy(3, TimeValue.ofSeconds(3)))
            .build();

    httpClient.execute(httpGet, response -> {
        System.out.println(response.getCode() + " " + response.getReasonPhrase());
        return null;
    });
}

If you want to see more custom strategy which retries all check here.