Artem Yarulin
Artem Yarulin

Reputation: 539

Spring WebFlux WebClient hangs and Mono.timeout doesn't catch it

I have rather simple Kotlin code which always blocks and never returns:

WebClient
        .create()
        .get()
        .uri("https://httpbin.org/status/200")
        .exchange()
        .flatMap {
            println("Status ${it.statusCode()}")
            it.bodyToMono(String::class.java)
        }
        .timeout(Duration.ofSeconds(5), Mono.just("fallback"))
        .map { println("Response $it") } // never runs
        .block()

Upvotes: 3

Views: 4899

Answers (2)

user3675334
user3675334

Reputation: 61

I have the same question. After reading the answer from Brain I think the real problem still can't be resolved. Actually we need to take action for response without body, e.g. reply with a Mono.just (just like Artem want to do after timeout). Nowtimeout() can't handle this case, but doOnSuccess() seems can't handle this case as well because it will be called for either having body or no body. In the doOnSuccess() we still can't know whether the clientReponse has a body or not then take different action.

And as Brian said before doOn** has side-effects is only for log usage.

Upvotes: 0

Brian Clozel
Brian Clozel

Reputation: 59056

In this case, httpbin.org does not return a response body, so the bodyToMono method will return the equivalent of Mono.empty().

If you change the map { println("Response $it") } (which will never run, since wee don't get a body) into a doOnSuccess { log.info("Response received") }, the "Response received" message is printed.

This means that the Mono is not stuck, it just completed with an empty body.

Upvotes: 2

Related Questions