1717package org .springframework .boot .web .embedded .netty ;
1818
1919import java .net .BindException ;
20- import java .util .concurrent .CountDownLatch ;
21- import java .util .concurrent .atomic .AtomicReference ;
2220
2321import org .apache .commons .logging .Log ;
2422import org .apache .commons .logging .LogFactory ;
25- import reactor .ipc .netty .NettyContext ;
2623import reactor .ipc .netty .http .server .HttpServer ;
24+ import reactor .ipc .netty .tcp .BlockingNettyContext ;
2725
2826import org .springframework .boot .web .server .WebServer ;
2927import org .springframework .boot .web .server .WebServerException ;
@@ -42,13 +40,11 @@ public class NettyWebServer implements WebServer {
4240
4341 private static final Log logger = LogFactory .getLog (NettyWebServer .class );
4442
45- private CountDownLatch latch ;
46-
4743 private final ReactorHttpHandlerAdapter handlerAdapter ;
4844
4945 private final HttpServer reactorServer ;
5046
51- private AtomicReference < NettyContext > nettyContext = new AtomicReference <>() ;
47+ private BlockingNettyContext nettyContext ;
5248
5349 public NettyWebServer (HttpServer reactorServer ,
5450 ReactorHttpHandlerAdapter handlerAdapter ) {
@@ -58,11 +54,9 @@ public NettyWebServer(HttpServer reactorServer,
5854
5955 @ Override
6056 public void start () throws WebServerException {
61- if (this .nettyContext .get () == null ) {
62- this .latch = new CountDownLatch (1 );
57+ if (this .nettyContext == null ) {
6358 try {
64- this .nettyContext
65- .set (this .reactorServer .newHandler (this .handlerAdapter ).block ());
59+ this .nettyContext = this .reactorServer .start (this .handlerAdapter );
6660 }
6761 catch (Exception ex ) {
6862 if (findBindException (ex ) != null ) {
@@ -71,7 +65,7 @@ public void start() throws WebServerException {
7165 throw new WebServerException ("Unable to start Netty" , ex );
7266 }
7367 NettyWebServer .logger .info ("Netty started on port(s): " + getPort ());
74- startDaemonAwaitThread ();
68+ startDaemonAwaitThread (this . nettyContext );
7569 }
7670 }
7771
@@ -86,16 +80,12 @@ private BindException findBindException(Exception ex) {
8680 return null ;
8781 }
8882
89- private void startDaemonAwaitThread () {
83+ private void startDaemonAwaitThread (BlockingNettyContext nettyContext ) {
9084 Thread awaitThread = new Thread ("server" ) {
9185
9286 @ Override
9387 public void run () {
94- try {
95- NettyWebServer .this .latch .await ();
96- }
97- catch (InterruptedException e ) {
98- }
88+ nettyContext .getContext ().onClose ().block ();
9989 }
10090
10191 };
@@ -106,19 +96,16 @@ public void run() {
10696
10797 @ Override
10898 public void stop () throws WebServerException {
109- NettyContext context = this .nettyContext .getAndSet (null );
110- if (context != null ) {
111- context .dispose ();
112- }
113- if (this .latch != null ) {
114- this .latch .countDown ();
99+ if (this .nettyContext != null ) {
100+ this .nettyContext .shutdown ();
101+ this .nettyContext = null ;
115102 }
116103 }
117104
118105 @ Override
119106 public int getPort () {
120- if (this .nettyContext . get () != null ) {
121- return this .nettyContext .get (). address (). getPort ();
107+ if (this .nettyContext != null ) {
108+ return this .nettyContext .getPort ();
122109 }
123110 return 0 ;
124111 }
0 commit comments