3

I have following WebClient - makes http call to localhost:8090 - bean defined:

@Configuration
class WebClientConfig {
    @Bean
    public WebClient webClientBean() {
        return WebClient.create("http://localhost:8090");
    }
}

And a call another service (8090):

Response response = requestBodySpec
        .retrieve()
        .bodyToMono(Response.class)
        .timeout(Duration.ofSeconds(5))
        .retry(2L)
        .doOnSuccess(res -> log.info(res))
        .doOnError(err -> log.error(err))
        .block();

I see the error after timing out:

java.util.concurrent.TimeoutException: Did not observe any item or terminal signal within 5000ms in 'flatMap'

But I am not seeing logs for the 2 additional retries that I specified; they are only visible when I turn on TRACE:

TRACE 2120 --- [nio-8080-exec-1] o.s.w.r.f.client.ExchangeFunctions : [9c7e1e0] HTTP POST http://localhost:8090/test, headers={masked}

Is there any way I can log the event whenever there is a retry happening? Similar to above log except I have to INFO log it.

夢のの夢
  • 5,054
  • 7
  • 33
  • 63

1 Answers1

5

You can try specifying a RetrySpec instead, which gives more control over the retry behavior and provides a "callback" for doing non-blocking things.

Assuming your logger is non-blocking you can do something like this:

RetryBackOffSpec retrySpec = Retry.fixedDelay(2L, Duration.ofMillis(25))
            .doAfterRetry(signal -> log.info("Retry number {}", signal.totalRetries()));

Response response = requestBodySpec
        .retrieve()
        .bodyToMono(Response.class)
        .timeout(Duration.ofSeconds(5))
        .retry(retrySpec)
        .doOnSuccess(res -> log.info(res))
        .doOnError(err -> log.error(err))
        .block();
Joe B
  • 1,261
  • 14
  • 20