background
I was going to implement graceful shutdown for my project (http using webclient), and when I tried to shutdown my application, many http tasks would be still put into the queue of webclient. and I would like to execute completely these http request as best as it can, but PrematureCloseException would encounter at this time.
o.s.w.r.f.c.WebClientRequestException: Connection prematurely closed BEFORE response
at o.s.w.r.f.c.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:136)
Suppressed: r.c.p.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
*__checkpoint ⇢ Request to PUT http://xxx [DefaultWebClient]
Original Stack Trace:
at o.s.w.r.f.c.ExchangeFunctions$DefaultExchangeFunction.lambda$wrapException$9(ExchangeFunctions.java:136)
at r.c.p.MonoErrorSupplied.subscribe(MonoErrorSupplied.java:55)
at r.c.p.Mono.subscribe(Mono.java:4568)
at r.c.p.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:103)
at r.c.p.FluxPeek$PeekSubscriber.onError(FluxPeek.java:222)
at r.c.p.FluxPeek$PeekSubscriber.onError(FluxPeek.java:222)
at r.c.p.FluxPeek$PeekSubscriber.onError(FluxPeek.java:222)
at r.c.p.MonoNext$NextSubscriber.onError(MonoNext.java:93)
at r.c.p.MonoFlatMapMany$FlatMapManyMain.onError(MonoFlatMapMany.java:205)
at r.c.p.SerializedSubscriber.onError(SerializedSubscriber.java:124)
at r.c.p.FluxRetryWhen$RetryWhenMainSubscriber.whenError(FluxRetryWhen.java:229)
at r.c.p.FluxRetryWhen$RetryWhenOtherSubscriber.onError(FluxRetryWhen.java:279)
at r.c.p.FluxContextWrite$ContextWriteSubscriber.onError(FluxContextWrite.java:121)
at r.c.p.FluxConcatMapNoPrefetch$FluxConcatMapNoPrefetchSubscriber.maybeOnError(FluxConcatMapNoPrefetch.java:327)
at r.c.p.FluxConcatMapNoPrefetch$FluxConcatMapNoPrefetchSubscriber.onNext(FluxConcatMapNoPrefetch.java:212)
at r.c.p.FluxContextWrite$ContextWriteSubscriber.onNext(FluxContextWrite.java:107)
at r.c.p.SinkManyEmitterProcessor.drain(SinkManyEmitterProcessor.java:476)
at r.c.p.SinkManyEmitterProcessor$EmitterInner.drainParent(SinkManyEmitterProcessor.java:620)
at r.c.p.FluxPublish$PubSubInner.request(FluxPublish.java:874)
at r.c.p.FluxContextWrite$ContextWriteSubscriber.request(FluxContextWrite.java:136)
... 42 frames truncated
Caused by: r.n.h.c.PrematureCloseException: Connection prematurely closed BEFORE response
I’m not sure why this error encountered, after some investigation, I guessed the connection would be closed by netty when the application got the shutdown signal once, handled in the org.springframework.http.client.ReactorResourceFactory#stop
.
although I have implemented the graceful shutdown method for my own logic like :
@Bean(initMethod = "start", destroyMethod = "shutdown")
but during debug, the error already encountered always before the execution of destroyMethod = "shutdown"
below is the bean of the webclient:
@Bean(name = "webClient")
public WebClient webClient(WebClient.Builder builder) {
HttpClient httpClient = HttpClient.create().responseTimeout(5);
return builder.clientConnector(new ReactorClientHttpConnector(httpClient)).build();
}
Question
how can I avoid this error so that I can make it best to complete the http requests to achieve the graceful shutdown
user143715 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.