I am new to spring web flux, I have a client application that consumes server-sent events, The events are published by the server randomly there is not fixed delay. But consumer throws io.netty.handler.timeout.ReadTimeoutException: null after 60 secs if there no event
Server-side events consumer code
webClient.get()
.uri("http://localhost:8080/events")
.accept(MediaType.TEXT_EVENT_STREAM)
.retrieve()
.bodyToFlux(type)
.subscribe(event -> process(event));
I need the client to be connected even if there is no event for a long time.
Full Exception
[36mr.netty.http.client.HttpClientConnect [...] The connection observed an error
io.netty.handler.timeout.ReadTimeoutException: null
reactor.Flux.MonoFlatMapMany.1 onError(org.springframework.web.reactive.function.client.WebClientRequestException: nested exception is io.netty.handler.timeout.ReadTimeoutException)
reactor.Flux.MonoFlatMapMany.1
org.springframework.web.reactive.function.client.WebClientRequestException: nested exception is io.netty.handler.timeout.ReadTimeoutException
at org.springframework.web.reactive.function.client.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:141) ~[spring-webflux-5.3.5.jar:5.3.5]
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
Webflux use a default timeout fallback that will eventually show io.netty.handler.timeout.ReadTimeoutException: null.
It is possible to prevent this error by passing a custom timeout fallback to the timeout method(s):
public final Flux<T> timeout(Duration timeout, @Nullable Publisher<? extends T> fallback);
Additionally, you can use methods like onErrorContinue, onErrorReturn, ... to properly handle exceptions in Flux, example:
return webClient.get().uri(url).retrieve().bodyToFlux(String.class)
.timeout(timeout, Mono.error(new ReadTimeoutException("Timeout")))
.onErrorContinue((e, i) -> {
// Log the error here.
});
If you want to disable all these logs by default it is possible adding this line into file application.properties:
logging.level.reactor.netty.http.client.HttpClient=OFF
In my case the problem was the HttpClient response timeout, I just increased it
HttpClient.create(...).responseTimeout(Duration.ofMillis(5000));
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