Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging retries with Reactor Mono

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"));
like image 621
codebox Avatar asked Sep 20 '19 09:09

codebox


Video Answer


2 Answers

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());
        }));
like image 162
Hille Avatar answered Oct 18 '22 04:10

Hille


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")))
like image 45
codebox Avatar answered Oct 18 '22 04:10

codebox