I am running a Spring boot 3.0.1 application, using WebClient to manage HTTP requests (switching from RestTemplate). I am using WebClient in a blocking way - sending my requests like this:
webClient.get().uri(path).headers(headers).retrieve().bodyToMono(responseType).block();
Where path
, headers
, and responseType
are already defined. Here is how I declare the WebClient Spring Bean:
@Bean
public WebClient webClient(CloseableHttpAsyncClient httpAsyncClient) {
return WebClient.builder()
.clientConnector(new HttpComponentsClientHttpConnector(httpAsyncClient))
.filter(new CustomResponseHeaderFilter())
.build();
}
Here's the implementation of the CustomResponseHeaderFilter
class. (Yes I know it's not doing anything right now):
public class CustomResponseHeaderFilter implements ExchangeFilterFunction {
@Override
public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
return next.exchange(request);
}
@Override
public ExchangeFilterFunction andThen(ExchangeFilterFunction afterFilter) {
return ExchangeFilterFunction.super.andThen(afterFilter);
}
@Override
public ExchangeFunction apply(ExchangeFunction exchange) {
return ExchangeFilterFunction.super.apply(exchange);
}
}
So in the current state of my application, does the code that's run throughout an entire request run on the same thread? I've seen some information from another post here saying that the thread pauses, and in the background a separate thread is used for network operations.
More specifically, does the block()
method call affect how the ExchangeFilterFunction
is executed? What I mean by that is does the code in the .filter()
method also run on the same thread as the request since we are using block()
?
I haven't seen any documentation so far regarding blocking and the filter function, and I'm not entirely sure how this works behind the scenes.
UPDATE:
This is directly relating to using MDC
. Essentially I am wondering whether MDC can be used as if the entire request is run on 1 thread - like when it's used in RestTemplate
. I am wondering if I add a value to the MDC context in the logResponse
filter function while using .block()
, will it be available to use after the request has been processed?
Thanks