Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DemoApplication {
- private WebClient webclient = WebClient.create("http://localhost:8080/");
- public static void main(String[] args) throws Exception {
- new DemoApplication().startServer();
- }
- public void startServer() throws Exception {
- RouterFunction<ServerResponse> route = routingFunction();
- HttpHandler httpHandler = RouterFunctions.toHttpHandler(route);
- ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
- HttpServer server = HttpServer.create("127.0.0.1", 8080);
- server.newHandler(adapter).block();
- Thread.sleep(1000000);
- }
- public RouterFunction<ServerResponse> routingFunction() throws Exception {
- return route(path("/1"), req -> ok().body(fromPublisher(get1(), String.class)))
- .andRoute(path("/2"), req -> ok().body(fromPublisher(get2(), String.class)))
- .andRoute(path("/3"), req -> ok().body(fromPublisher(get3(), String.class)));
- }
- public Mono<String> get1() {
- System.out.println("---------REQUEST---------");
- System.out.println("1: " + Thread.currentThread());
- return webclient.get().uri("2").retrieve().bodyToMono(String.class);
- }
- public Mono<String> get2() {
- System.out.println("2: " + Thread.currentThread());
- return webclient.get().uri("3").retrieve().bodyToMono(String.class);
- }
- public Mono<String> get3() {
- System.out.println("3: " + Thread.currentThread());
- try {
- Thread.sleep(1250000); // simulate thread somehow got blocked
- } catch (InterruptedException e) {
- }
- return Mono.just("test");
- }
- }
- ---------REQUEST---------
- 1: Thread[reactor-http-nio-2,5,main]
- 2: Thread[reactor-http-nio-4,5,main]
- 3: Thread[reactor-http-nio-2,5,main]
- ---------REQUEST---------
- 1: Thread[reactor-http-nio-3,5,main]
- 2: Thread[reactor-http-nio-1,5,main]
- ---------REQUEST---------
- 1: Thread[reactor-http-nio-3,5,main]
- 2: Thread[reactor-http-nio-1,5,main]
- ---------REQUEST---------
- 1: Thread[reactor-http-nio-3,5,main]
- 2: Thread[reactor-http-nio-1,5,main]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement