Advertisement
Guest User

Untitled

a guest
Jun 17th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. public class DemoApplication {
  2.  
  3. private WebClient webclient = WebClient.create("http://localhost:8080/");
  4.  
  5. public static void main(String[] args) throws Exception {
  6. new DemoApplication().startServer();
  7. }
  8.  
  9. public void startServer() throws Exception {
  10. RouterFunction<ServerResponse> route = routingFunction();
  11. HttpHandler httpHandler = RouterFunctions.toHttpHandler(route);
  12. ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler);
  13. HttpServer server = HttpServer.create("127.0.0.1", 8080);
  14. server.newHandler(adapter).block();
  15. Thread.sleep(1000000);
  16. }
  17.  
  18. public RouterFunction<ServerResponse> routingFunction() throws Exception {
  19. return route(path("/1"), req -> ok().body(fromPublisher(get1(), String.class)))
  20. .andRoute(path("/2"), req -> ok().body(fromPublisher(get2(), String.class)))
  21. .andRoute(path("/3"), req -> ok().body(fromPublisher(get3(), String.class)));
  22. }
  23.  
  24. public Mono<String> get1() {
  25. System.out.println("---------REQUEST---------");
  26. System.out.println("1: " + Thread.currentThread());
  27. return webclient.get().uri("2").retrieve().bodyToMono(String.class);
  28. }
  29.  
  30. public Mono<String> get2() {
  31. System.out.println("2: " + Thread.currentThread());
  32. return webclient.get().uri("3").retrieve().bodyToMono(String.class);
  33. }
  34.  
  35. public Mono<String> get3() {
  36. System.out.println("3: " + Thread.currentThread());
  37. try {
  38. Thread.sleep(1250000); // simulate thread somehow got blocked
  39. } catch (InterruptedException e) {
  40.  
  41. }
  42. return Mono.just("test");
  43. }
  44. }
  45.  
  46. ---------REQUEST---------
  47. 1: Thread[reactor-http-nio-2,5,main]
  48. 2: Thread[reactor-http-nio-4,5,main]
  49. 3: Thread[reactor-http-nio-2,5,main]
  50. ---------REQUEST---------
  51. 1: Thread[reactor-http-nio-3,5,main]
  52. 2: Thread[reactor-http-nio-1,5,main]
  53. ---------REQUEST---------
  54. 1: Thread[reactor-http-nio-3,5,main]
  55. 2: Thread[reactor-http-nio-1,5,main]
  56. ---------REQUEST---------
  57. 1: Thread[reactor-http-nio-3,5,main]
  58. 2: Thread[reactor-http-nio-1,5,main]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement