Reputation: 539
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()
httpbin
in this case returns no content (only headers), so is that so that bodyToMono
waits forever for the content here? Is there solid way how to handle such case (reading Content-Length is not reliable)
I set Mono.timeout with fallback value, why doesn't it cover that?
Upvotes: 3
Views: 4899
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
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