How to set a timeout in Spring 5 WebFlux WebClient

前端 未结 7 2002
暗喜
暗喜 2020-12-01 06:30

I\'m trying to set timeout on my WebClient, here is the current code :

SslContext sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManag         


        
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 07:08

    As Spring Webflux was updated, here is a solution that works for Java (based on the answer for Kotlin):

    TcpClient timeoutClient = TcpClient.create()
        .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, SECONDS*1000)
        .doOnConnected(
            c -> c.addHandlerLast(new ReadTimeoutHandler(SECONDS))
                  .addHandlerLast(new WriteTimeoutHandler(SECONDS)));
    return webClientBuilder.baseUrl(YOUR_URL)
           .clientConnector(new ReactorClientHttpConnector(HttpClient.from(timeoutClient)))
           .build();
    

提交回复
热议问题