Log the port used by Netty

Closes gh-8681
pull/8491/merge
Madhura Bhave 8 years ago
parent f07d4f77aa
commit cfb1defb9e

@ -16,9 +16,12 @@
package org.springframework.boot.web.embedded.netty; package org.springframework.boot.web.embedded.netty;
import java.net.BindException;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.Loopback; import reactor.core.Loopback;
import reactor.ipc.netty.NettyContext; import reactor.ipc.netty.NettyContext;
import reactor.ipc.netty.http.server.HttpServer; import reactor.ipc.netty.http.server.HttpServer;
@ -33,10 +36,13 @@ import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter;
* directly. * directly.
* *
* @author Brian Clozel * @author Brian Clozel
* @author Madhura Bhave
* @since 2.0.0 * @since 2.0.0
*/ */
public class NettyWebServer implements WebServer, Loopback { public class NettyWebServer implements WebServer, Loopback {
private static final Log logger = LogFactory.getLog(NettyWebServer.class);
private static CountDownLatch latch = new CountDownLatch(1); private static CountDownLatch latch = new CountDownLatch(1);
private final ReactorHttpHandlerAdapter handlerAdapter; private final ReactorHttpHandlerAdapter handlerAdapter;
@ -64,12 +70,32 @@ public class NettyWebServer implements WebServer, Loopback {
@Override @Override
public void start() throws WebServerException { public void start() throws WebServerException {
if (this.nettyContext.get() == null) { if (this.nettyContext.get() == null) {
try {
this.nettyContext this.nettyContext
.set(this.reactorServer.newHandler(this.handlerAdapter).block()); .set(this.reactorServer.newHandler(this.handlerAdapter).block());
}
catch (Exception ex) {
if (findBindException(ex) != null) {
// throw new PortInUseException();
}
throw new WebServerException("Unable to start Netty", ex);
}
NettyWebServer.logger.info("Netty started on port(s): " + getPort());
startDaemonAwaitThread(); startDaemonAwaitThread();
} }
} }
private BindException findBindException(Exception ex) {
Throwable candidate = ex;
while (candidate != null) {
if (candidate instanceof BindException) {
return (BindException) candidate;
}
candidate = candidate.getCause();
}
return null;
}
private void startDaemonAwaitThread() { private void startDaemonAwaitThread() {
Thread awaitThread = new Thread("server") { Thread awaitThread = new Thread("server") {

@ -88,6 +88,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
public void startStopServer() { public void startStopServer() {
this.webServer = getFactory().getWebServer(new EchoHandler()); this.webServer = getFactory().getWebServer(new EchoHandler());
this.webServer.start(); this.webServer.start();
assertThat(this.output.toString()).contains("started on port");
Mono<String> result = getWebClient().post().uri("/test") Mono<String> result = getWebClient().post().uri("/test")
.contentType(MediaType.TEXT_PLAIN) .contentType(MediaType.TEXT_PLAIN)
.exchange(BodyInserters.fromObject("Hello World")) .exchange(BodyInserters.fromObject("Hello World"))

Loading…
Cancel
Save