Jelajahi Sumber

Revert "webflux-form sample uses Tomcat"

This reverts commit 67bb91bb76beb6d1d6e5118770217f66b4074c86.

We can use Netty again now that reactor/reactor-netty#248 has
been resolved.

Fixes: gh-4923
Rob Winch 7 tahun lalu
induk
melakukan
fe40952908

+ 2 - 1
samples/javaconfig/webflux-form/spring-security-samples-javaconfig-webflux-form.gradle

@@ -20,8 +20,9 @@ dependencies {
 	compile project(':spring-security-core')
 	compile project(':spring-security-config')
 	compile project(':spring-security-web')
-	compile 'org.apache.tomcat.embed:tomcat-embed-core'
 	compile 'com.fasterxml.jackson.core:jackson-databind'
+	compile 'io.netty:netty-buffer'
+	compile 'io.projectreactor.ipc:reactor-netty'
 	compile 'org.springframework:spring-context'
 	compile 'org.springframework:spring-webflux'
 	compile 'org.thymeleaf:thymeleaf-spring5'

+ 2 - 13
samples/javaconfig/webflux-form/src/integration-test/java/sample/WebfluxFormApplicationTests.java

@@ -28,20 +28,17 @@ import org.springframework.test.context.junit4.SpringRunner;
 import sample.webdriver.IndexPage;
 import sample.webdriver.LoginPage;
 
-import java.io.IOException;
-import java.net.ServerSocket;
-
 /**
  * @author Rob Winch
  * @since 5.0
  */
 @RunWith(SpringRunner.class)
 @ContextConfiguration(classes = WebfluxFormApplication.class)
-@TestPropertySource(properties = "server.port=#{T(sample.WebfluxFormApplicationTests).availablePort()}")
+@TestPropertySource(properties = "server.port=0")
 public class WebfluxFormApplicationTests {
 	WebDriver driver;
 
-	@Value("#{@tomcat.server.port}")
+	@Value("#{@nettyContext.address().getPort()}")
 	int port;
 
 	@Before
@@ -79,12 +76,4 @@ public class WebfluxFormApplicationTests {
 			.assertAt()
 			.assertLogout();
 	}
-
-	public static final int availablePort() {
-		try(ServerSocket socket = new ServerSocket(0)) {
-			return socket.getLocalPort();
-		} catch(IOException e) {
-			throw new RuntimeException(e);
-		}
-	}
 }

+ 11 - 23
samples/javaconfig/webflux-form/src/main/java/sample/WebfluxFormApplication.java

@@ -16,20 +16,15 @@
 
 package sample;
 
-import org.apache.catalina.Context;
-import org.apache.catalina.startup.Tomcat;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.ApplicationContext;
-import org.springframework.context.annotation.AnnotationConfigApplicationContext;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.ComponentScan;
-import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.*;
 import org.springframework.http.server.reactive.HttpHandler;
-import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
+import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
 import org.springframework.web.reactive.config.EnableWebFlux;
 import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
-
-import javax.servlet.Servlet;
+import reactor.ipc.netty.NettyContext;
+import reactor.ipc.netty.http.server.HttpServer;
 
 /**
  * @author Rob Winch
@@ -43,26 +38,19 @@ public class WebfluxFormApplication {
 	private int port = 8080;
 
 	public static void main(String[] args) throws Exception {
-		Object lock = new Object();
 		try(AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
 			WebfluxFormApplication.class)) {
-			synchronized (lock) {
-				lock.wait();
-			}
+			context.getBean(NettyContext.class).onClose().block();
 		}
 	}
 
-	@Bean(destroyMethod = "stop", initMethod = "start")
-	public Tomcat tomcat(ApplicationContext context) throws Exception {
+	@Profile("default")
+	@Bean
+	public NettyContext nettyContext(ApplicationContext context) {
 		HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context)
 			.build();
-		Servlet servlet = new ServletHttpHandlerAdapter(handler);
-		Tomcat server = new Tomcat();
-		server.setPort(this.port);
-		server.getServer().setPort(this.port);
-		Context rootContext = server.addContext("", System.getProperty("java.io.tmpdir"));
-		Tomcat.addServlet(rootContext, "servlet", servlet);
-		rootContext.addServletMapping("/", "servlet");
-		return server;
+		ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
+		HttpServer httpServer = HttpServer.create("localhost", port);
+		return httpServer.newHandler(adapter).block();
 	}
 }