I'm using Reactive Streams to call a web service, and have configured an automatic retry policy so that if the initial call fails a number of retries will be attempted. I would like to be able to log each of the retries, but can't find a way to do this in the documentation. The Mono object has doOn...
methods for various events such as success, failure etc but I can't see doOnRetry
. Can anyone suggest a way to do this? My code looks something like this:
return webClient.get()
.uri(myUrl)
.retrieve()
.bodyToMono(byte[].class)
.retryBackoff(RETRIES, MIN_BACKOFF_DURATION, MAX_BACKOFF_DURATION)
.doOnSubscribe(subscription -> LOGGER.info("subscribe"))
.doOnSuccess(result -> LOGGER.info("success"))
//.doOnRetry(something -> LOGGER.info("retry")) <-- I want to do this
.doOnError(err -> LOGGER.info("error"));
Starting with Reactor Core, Version 3.3.4 you may use the overhauled retryWhen
model; like:
[...]
.retryWhen(Retry.backoff(5, Duration.ofSeconds(2))
.doBeforeRetry(retrySignal -> {
log.error("Retrying: "
+ retrySignal.totalRetries() + "; "
+ retrySignal.totalRetriesInARow() + "; "
+ retrySignal.failure());
}));
Ok, I found a way to do this by adding the Reactor Extra Utilities package to my project. This allowed me to replace the retryBackoff
method in the above example with the following:
.retryWhen(Retry.any()
.retryMax(RETRIES)
.exponentialBackoff(MIN_BACKOFF_DURATION, MAX_BACKOFF_DURATION)
.doOnRetry(context -> LOGGER.warn("retry")))
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