Currently we have a global webclient configured at the common base library level (cannot be modified) and it has global timeouts that are applied to all the requests. It is using Reactor Netty.

    @Bean
    public WebClientCustomizer getWeebClientCustomizer(Optional<SslContextProvider> sslContextProvider) {

        HttpClient httpClient = HttpClient.create(ConnectionProvider.builder("webclient-connection-provider").build())
        .option(ChannelOption.TCP_NODELAY, true)
        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000).runOn(LoopResources.create("webclient-event-loop"))
        .keepAlive(Boolean.TRUE).wiretap(Boolean.TRUE)
        .doOnConnected(conn -> conn
              .addHandler(new ReadTimeoutHandler(10, TimeUnit.SECONDS))
              .addHandlerLast(new WriteTimeoutHandler(10, TimeUnit.SECONDS)))
              .responseTimeout(Duration.ofSeconds(10));  

        HttpClient httpClientSSL =  sslContextProvider.map(SslContextProvider::sslContext)
        .map(s -> httpClient.secure(t -> t.sslContext(s).handshakeTimeoutMillis(5000)
        .orElse(httpClient);

        ClientHttpConnector connector = new ReactorClientHttpConnector(httpClient.wiretap(true));

        return webclientBuilder -> webclientBuilder.clientConnector(connector);
    }

    @Bean
    public WebClient getWebClient(WebClient.Builder webclientBuilder) {
        return webclientBuilder.build();
    }

Because of above global configurations set up, we are getting Out of Memomy exceptions in PRODUCTION ENVIROMENT.

Now we want to have multiple webclients, each with different connection timeouts configurations in each instance of servers. I am reading various stackoverflow posts stating that we will need to use @Qualifier to inject those webclients or have some factory bean that takes WebClient.Builder and returns the webclient with respective customizer.

What would be the better solution to achieve my usecase. And will there be any performance issues having multiple webclients in the same instance?

Comment From: wilkinsona

I would inject the WebClient.Builder into each component that needs a web client and then have each of them create a customized WebClient from that Builder. I wouldn't bother with factory beans or @Qualifier as it sounds unnecessarily complicated unless you have many components that should all share the same client.

If you have any further questions, please follow up on Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

Comment From: gitsuren

Thank you so much @wilkinsona for your response. That really helps and we are a framework Team too and for our customer we are providing ways to our app team to allow them to create the webclients based on their need via custom WebClientFactory with builder injected and reading various props like ssl enabled, custom timeouts etc and is completely optional. Thanks much once again for your time to respond - really appreciate it!