webflux-form sample uses Tomcat

Work around gh-4923
This commit is contained in:
Rob Winch 2017-12-20 15:08:38 -06:00
parent 268a1dc06e
commit 67bb91bb76
3 changed files with 37 additions and 15 deletions

View File

@ -20,9 +20,8 @@ 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'

View File

@ -28,17 +28,20 @@ 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=0")
@TestPropertySource(properties = "server.port=#{T(sample.WebfluxFormApplicationTests).availablePort()}")
public class WebfluxFormApplicationTests {
WebDriver driver;
@Value("#{@nettyContext.address().getPort()}")
@Value("#{@tomcat.server.port}")
int port;
@Before
@ -76,4 +79,12 @@ 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);
}
}
}

View File

@ -16,15 +16,20 @@
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.*;
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.http.server.reactive.HttpHandler;
import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import reactor.ipc.netty.NettyContext;
import reactor.ipc.netty.http.server.HttpServer;
import javax.servlet.Servlet;
/**
* @author Rob Winch
@ -38,19 +43,26 @@ 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)) {
context.getBean(NettyContext.class).onClose().block();
synchronized (lock) {
lock.wait();
}
}
}
@Profile("default")
@Bean
public NettyContext nettyContext(ApplicationContext context) {
@Bean(destroyMethod = "stop", initMethod = "start")
public Tomcat tomcat(ApplicationContext context) throws Exception {
HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context)
.build();
ReactorHttpHandlerAdapter adapter = new ReactorHttpHandlerAdapter(handler);
HttpServer httpServer = HttpServer.create("localhost", port);
return httpServer.newHandler(adapter).block();
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;
}
}