Added timeout WebClient configuration, as in article
This commit is contained in:
parent
52d458c05f
commit
dd60782928
|
@ -4,6 +4,7 @@ import java.net.URI;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.reactivestreams.Publisher;
|
import org.reactivestreams.Publisher;
|
||||||
import org.reactivestreams.Subscriber;
|
import org.reactivestreams.Subscriber;
|
||||||
|
@ -12,6 +13,7 @@ import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ReactiveHttpOutputMessage;
|
import org.springframework.http.ReactiveHttpOutputMessage;
|
||||||
|
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
@ -20,7 +22,12 @@ import org.springframework.web.reactive.function.BodyInserter;
|
||||||
import org.springframework.web.reactive.function.BodyInserters;
|
import org.springframework.web.reactive.function.BodyInserters;
|
||||||
import org.springframework.web.reactive.function.client.WebClient;
|
import org.springframework.web.reactive.function.client.WebClient;
|
||||||
|
|
||||||
|
import io.netty.channel.ChannelOption;
|
||||||
|
import io.netty.handler.timeout.ReadTimeoutHandler;
|
||||||
|
import io.netty.handler.timeout.WriteTimeoutHandler;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
import reactor.netty.http.client.HttpClient;
|
||||||
|
import reactor.netty.tcp.TcpClient;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class WebClientController {
|
public class WebClientController {
|
||||||
|
@ -75,6 +82,19 @@ public class WebClientController {
|
||||||
return WebClient.create("http://localhost:8081");
|
return WebClient.create("http://localhost:8081");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private WebClient createWebClientConfiguringTimeout() {
|
||||||
|
TcpClient tcpClient = TcpClient.create()
|
||||||
|
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
|
||||||
|
.doOnConnected(connection -> {
|
||||||
|
connection.addHandlerLast(new ReadTimeoutHandler(5000, TimeUnit.MILLISECONDS));
|
||||||
|
connection.addHandlerLast(new WriteTimeoutHandler(5000, TimeUnit.MILLISECONDS));
|
||||||
|
});
|
||||||
|
|
||||||
|
return WebClient.builder()
|
||||||
|
.clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient)))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
private WebClient createWebClientWithServerURLAndDefaultValues() {
|
private WebClient createWebClientWithServerURLAndDefaultValues() {
|
||||||
return WebClient.builder()
|
return WebClient.builder()
|
||||||
.baseUrl("http://localhost:8081")
|
.baseUrl("http://localhost:8081")
|
||||||
|
|
Loading…
Reference in New Issue