javaspring-boottimeoutspring-webfluxwebclient

Java Specific webclient connect timeout per request


I have to call different urls with different connection timeout with webclient. But i found that we can set connect timeout globally only and not per resquest .. what to do please to set this timeout on a request without creating a new weblient each time.


Solution

  • You need to instantiate one webclient per url. For each webclient, you can set the connection timeout :

    HttpClient httpClient = HttpClient.create()
      .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
    WebClient client = WebClient.builder()
      .baseUrl("http://yourendpoint:8080")
      .clientConnector(new ReactorClientHttpConnector(httpClient))
      .build();
    

    Then reuse this webclient for all the calls to the configured url.