How to implement a retry mechanism in jersey-client java

ѕтƒ picture ѕтƒ · Jul 27, 2015 · Viewed 13.4k times · Source

I am doing some http rest api calls using jersey-client. Now I want to do a retry for a failure request. Say if the return error code is not 200 then I want to retry it again for a few times. How can do it using Jersey client

Answer

Jonathan picture Jonathan · Jul 31, 2015

For implementing retries in any situation, check out Failsafe:

RetryPolicy retryPolicy = new RetryPolicy()
  .retryIf((ClientResponse response) -> response.getStatus() != 200)
  .withDelay(1, TimeUnit.SECONDS)
  .withMaxRetries(3);

Failsafe.with(retryPolicy).get(() -> webResource.post(ClientResponse.class, input));

This example retries if the response status != 200, up to 3 times, with a 1 second delay between retries.