From bc036fc39d131abfd4e5f563b44c6a12ac754dda Mon Sep 17 00:00:00 2001 From: amit2103 Date: Sun, 29 Jul 2018 14:16:04 +0530 Subject: [PATCH] [BAEL-7608] - Reverted NettyContext to Embedded Tomcat example with Async = true --- .../functional/FunctionalWebApplication.java | 27 ++++++++++++++----- ...Spring5URLPatternUsingRouterFunctions.java | 27 +++++++++++++------ ...nctionalWebApplicationIntegrationTest.java | 12 +++------ ...rnUsingRouterFunctionsIntegrationTest.java | 8 +++--- 4 files changed, 47 insertions(+), 27 deletions(-) diff --git a/spring-5-reactive/src/main/java/com/baeldung/functional/FunctionalWebApplication.java b/spring-5-reactive/src/main/java/com/baeldung/functional/FunctionalWebApplication.java index 4e914ab65e..1f40798ada 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/functional/FunctionalWebApplication.java +++ b/spring-5-reactive/src/main/java/com/baeldung/functional/FunctionalWebApplication.java @@ -12,9 +12,14 @@ import java.util.Arrays; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; +import org.apache.catalina.Context; +import org.apache.catalina.Wrapper; +import org.apache.catalina.startup.Tomcat; +import org.springframework.boot.web.embedded.tomcat.TomcatWebServer; +import org.springframework.boot.web.server.WebServer; import org.springframework.core.io.ClassPathResource; import org.springframework.http.server.reactive.HttpHandler; -import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter; +import org.springframework.http.server.reactive.ServletHttpHandlerAdapter; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; @@ -22,8 +27,6 @@ import org.springframework.web.server.WebHandler; import org.springframework.web.server.adapter.WebHttpHandlerBuilder; import reactor.core.publisher.Flux; -import reactor.ipc.netty.NettyContext; -import reactor.ipc.netty.http.server.HttpServer; public class FunctionalWebApplication { @@ -48,15 +51,25 @@ public class FunctionalWebApplication { }); } - NettyContext start() throws Exception { + WebServer start() throws Exception { WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction()); HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler) .filter(new IndexRewriteFilter()) .build(); - ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler); - HttpServer server = HttpServer.create("localhost", 9090); - return server.newHandler(adapter).block(); + Tomcat tomcat = new Tomcat(); + tomcat.setHostname("localhost"); + tomcat.setPort(9090); + Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir")); + ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler); + Wrapper servletWrapper = Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet); + servletWrapper.setAsyncSupported(true); + rootContext.addServletMappingDecoded("/", "httpHandlerServlet"); + + TomcatWebServer server = new TomcatWebServer(tomcat); + server.start(); + return server; + } public static void main(String[] args) { diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctions.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctions.java index 0de1bcb539..115a057915 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctions.java +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctions.java @@ -6,18 +6,20 @@ import static org.springframework.web.reactive.function.server.RouterFunctions.r import static org.springframework.web.reactive.function.server.RouterFunctions.toHttpHandler; import static org.springframework.web.reactive.function.server.ServerResponse.ok; +import org.apache.catalina.Context; +import org.apache.catalina.Wrapper; +import org.apache.catalina.startup.Tomcat; +import org.springframework.boot.web.embedded.tomcat.TomcatWebServer; +import org.springframework.boot.web.server.WebServer; import org.springframework.core.io.ClassPathResource; import org.springframework.http.server.reactive.HttpHandler; -import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter; +import org.springframework.http.server.reactive.ServletHttpHandlerAdapter; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.RouterFunctions; import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.server.WebHandler; import org.springframework.web.server.adapter.WebHttpHandlerBuilder; -import reactor.ipc.netty.NettyContext; -import reactor.ipc.netty.http.server.HttpServer; - public class ExploreSpring5URLPatternUsingRouterFunctions { private RouterFunction routingFunction() { @@ -29,15 +31,24 @@ public class ExploreSpring5URLPatternUsingRouterFunctions { .and(RouterFunctions.resources("/files/{*filepaths}", new ClassPathResource("files/"))); } - NettyContext start() throws Exception { + WebServer start() throws Exception { WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction()); HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler) .filter(new IndexRewriteFilter()) .build(); - ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(httpHandler); - HttpServer server = HttpServer.create("localhost", 9090); - return server.newHandler(adapter).block(); + Tomcat tomcat = new Tomcat(); + tomcat.setHostname("localhost"); + tomcat.setPort(9090); + Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir")); + ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler); + Wrapper servletWrapper = Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet); + servletWrapper.setAsyncSupported(true); + rootContext.addServletMappingDecoded("/", "httpHandlerServlet"); + + TomcatWebServer server = new TomcatWebServer(tomcat); + server.start(); + return server; } diff --git a/spring-5-reactive/src/test/java/com/baeldung/functional/FunctionalWebApplicationIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/functional/FunctionalWebApplicationIntegrationTest.java index e780589333..4dea2a05cf 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/functional/FunctionalWebApplicationIntegrationTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/functional/FunctionalWebApplicationIntegrationTest.java @@ -3,11 +3,10 @@ package com.baeldung.functional; import static org.springframework.web.reactive.function.BodyInserters.fromObject; import static org.springframework.web.reactive.function.BodyInserters.fromResource; -import java.net.InetSocketAddress; - import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import org.springframework.boot.web.server.WebServer; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.http.MediaType; @@ -16,25 +15,22 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.reactive.function.BodyInserters; -import reactor.ipc.netty.NettyContext; - public class FunctionalWebApplicationIntegrationTest { private static WebTestClient client; - private static NettyContext server; + private static WebServer server; @BeforeClass public static void setup() throws Exception { server = new FunctionalWebApplication().start(); - InetSocketAddress serverAddress = server.address(); client = WebTestClient.bindToServer() - .baseUrl("http://" + serverAddress.getHostName() + ":" + serverAddress.getPort()) + .baseUrl("http://localhost:" + server.getPort()) .build(); } @AfterClass public static void destroy() { - server.dispose(); + server.stop(); } @Test diff --git a/spring-5-reactive/src/test/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest.java b/spring-5-reactive/src/test/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest.java index c9e1c59fdc..d735fd9f72 100644 --- a/spring-5-reactive/src/test/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest.java +++ b/spring-5-reactive/src/test/java/com/baeldung/reactive/urlmatch/ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest.java @@ -5,6 +5,7 @@ import java.net.InetSocketAddress; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import org.springframework.boot.web.server.WebServer; import org.springframework.test.web.reactive.server.WebTestClient; import reactor.ipc.netty.NettyContext; @@ -12,20 +13,19 @@ import reactor.ipc.netty.NettyContext; public class ExploreSpring5URLPatternUsingRouterFunctionsIntegrationTest { private static WebTestClient client; - private static NettyContext server; + private static WebServer server; @BeforeClass public static void setup() throws Exception { server = new ExploreSpring5URLPatternUsingRouterFunctions().start(); - InetSocketAddress serverAddress = server.address(); client = WebTestClient.bindToServer() - .baseUrl("http://" + serverAddress.getHostName() + ":" + serverAddress.getPort()) + .baseUrl("http://localhost:" + server.getPort()) .build(); } @AfterClass public static void destroy() { - server.dispose(); + server.stop(); } @Test