From 9f7f5c209b31d02ab227ca41714938dbc846c2a6 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 12 Jul 2017 15:26:05 +0200 Subject: [PATCH] Refactor NettyWebServer with BlockingNettyContext This commit leverages the new `BlockingNettyContext` in reactor-netty and simplifies the server lifecycle management. Closes gh-9698 --- .../web/embedded/netty/NettyWebServer.java | 37 ++++++------------- 1 file changed, 12 insertions(+), 25 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java b/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java index d4aeab8da2..c9bf5727a9 100644 --- a/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java +++ b/spring-boot/src/main/java/org/springframework/boot/web/embedded/netty/NettyWebServer.java @@ -17,13 +17,11 @@ package org.springframework.boot.web.embedded.netty; import java.net.BindException; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.atomic.AtomicReference; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import reactor.ipc.netty.NettyContext; import reactor.ipc.netty.http.server.HttpServer; +import reactor.ipc.netty.tcp.BlockingNettyContext; import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.server.WebServerException; @@ -42,13 +40,11 @@ public class NettyWebServer implements WebServer { private static final Log logger = LogFactory.getLog(NettyWebServer.class); - private CountDownLatch latch; - private final ReactorHttpHandlerAdapter handlerAdapter; private final HttpServer reactorServer; - private AtomicReference nettyContext = new AtomicReference<>(); + private BlockingNettyContext nettyContext; public NettyWebServer(HttpServer reactorServer, ReactorHttpHandlerAdapter handlerAdapter) { @@ -58,11 +54,9 @@ public class NettyWebServer implements WebServer { @Override public void start() throws WebServerException { - if (this.nettyContext.get() == null) { - this.latch = new CountDownLatch(1); + if (this.nettyContext == null) { try { - this.nettyContext - .set(this.reactorServer.newHandler(this.handlerAdapter).block()); + this.nettyContext = this.reactorServer.start(this.handlerAdapter); } catch (Exception ex) { if (findBindException(ex) != null) { @@ -71,7 +65,7 @@ public class NettyWebServer implements WebServer { throw new WebServerException("Unable to start Netty", ex); } NettyWebServer.logger.info("Netty started on port(s): " + getPort()); - startDaemonAwaitThread(); + startDaemonAwaitThread(this.nettyContext); } } @@ -86,16 +80,12 @@ public class NettyWebServer implements WebServer { return null; } - private void startDaemonAwaitThread() { + private void startDaemonAwaitThread(BlockingNettyContext nettyContext) { Thread awaitThread = new Thread("server") { @Override public void run() { - try { - NettyWebServer.this.latch.await(); - } - catch (InterruptedException e) { - } + nettyContext.getContext().onClose().block(); } }; @@ -106,19 +96,16 @@ public class NettyWebServer implements WebServer { @Override public void stop() throws WebServerException { - NettyContext context = this.nettyContext.getAndSet(null); - if (context != null) { - context.dispose(); - } - if (this.latch != null) { - this.latch.countDown(); + if (this.nettyContext != null) { + this.nettyContext.shutdown(); + this.nettyContext = null; } } @Override public int getPort() { - if (this.nettyContext.get() != null) { - return this.nettyContext.get().address().getPort(); + if (this.nettyContext != null) { + return this.nettyContext.getPort(); } return 0; }