Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a retry mechanism in jersey-client java

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

like image 440
ѕтƒ Avatar asked Jul 27 '15 10:07

ѕтƒ


People also ask

How do you create a retry mechanism in Java?

A simple solution to implement retry logic in Java is to write your code inside a for loop that executes the specified number of times (the maximum retry value).

What is retry in Java?

Retry pattern consists retrying operations on remote resources over the network a set number of times.


1 Answers

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.

like image 198
Jonathan Avatar answered Sep 16 '22 11:09

Jonathan