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
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).
Retry pattern consists retrying operations on remote resources over the network a set number of times.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With