From 6bbce910b8c7a42506451ddf3e9cbe9700bded79 Mon Sep 17 00:00:00 2001 From: graemerocher Date: Tue, 8 Jan 2019 11:59:49 +0100 Subject: [PATCH] Allow Netty to attempt to bind. Fixes #1081 --- .../http/server/netty/NettyHttpServer.java | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/http-server-netty/src/main/java/io/micronaut/http/server/netty/NettyHttpServer.java b/http-server-netty/src/main/java/io/micronaut/http/server/netty/NettyHttpServer.java index 44d9598ddcb..6eb4aa0db0d 100644 --- a/http-server-netty/src/main/java/io/micronaut/http/server/netty/NettyHttpServer.java +++ b/http-server-netty/src/main/java/io/micronaut/http/server/netty/NettyHttpServer.java @@ -311,24 +311,7 @@ protected void initChannel(Channel ch) { public synchronized EmbeddedServer stop() { if (isRunning() && workerGroup != null) { if (running.compareAndSet(true, false)) { - try { - workerGroup.shutdownGracefully() - .addListener(this::logShutdownErrorIfNecessary); - parentGroup.shutdownGracefully() - .addListener(this::logShutdownErrorIfNecessary); - webSocketSessions.close(); - applicationContext.publishEvent(new ServerShutdownEvent(this)); - if (serviceInstance != null) { - applicationContext.publishEvent(new ServiceShutdownEvent(serviceInstance)); - } - if (applicationContext.isRunning()) { - applicationContext.stop(); - } - } catch (Throwable e) { - if (LOG.isErrorEnabled()) { - LOG.error("Error stopping Micronaut server: " + e.getMessage(), e); - } - } + stopInternal(); } } return this; @@ -405,9 +388,6 @@ protected ServerBootstrap createServerBootstrap() { @SuppressWarnings("MagicNumber") private void bindServerToHost(ServerBootstrap serverBootstrap, @Nullable String host, AtomicInteger attempts) { boolean isRandomPort = specifiedPort == -1; - if (!SocketUtils.isTcpPortAvailable(serverPort) && !isRandomPort) { - throw new ServerStartupException("Unable to start Micronaut server on port: " + serverPort, new BindException("Address already in use")); - } Optional applicationName = serverConfiguration.getApplicationConfiguration().getName(); if (applicationName.isPresent()) { if (LOG.isDebugEnabled()) { @@ -433,8 +413,9 @@ private void bindServerToHost(ServerBootstrap serverBootstrap, @Nullable String }); } catch (Throwable e) { + final boolean isBindError = e instanceof BindException; if (LOG.isErrorEnabled()) { - if (e instanceof BindException) { + if (isBindError) { LOG.error("Unable to start server. Port already {} in use.", serverPort); } else { LOG.error("Error starting Micronaut server: " + e.getMessage(), e); @@ -446,7 +427,8 @@ private void bindServerToHost(ServerBootstrap serverBootstrap, @Nullable String serverPort = SocketUtils.findAvailableTcpPort(); bindServerToHost(serverBootstrap, host, attempts); } else { - stop(); + stopInternal(); + throw new ServerStartupException("Unable to start Micronaut server on port: " + serverPort, e); } } } @@ -460,6 +442,27 @@ private void logShutdownErrorIfNecessary(Future future) { } } + private void stopInternal() { + try { + workerGroup.shutdownGracefully() + .addListener(this::logShutdownErrorIfNecessary); + parentGroup.shutdownGracefully() + .addListener(this::logShutdownErrorIfNecessary); + webSocketSessions.close(); + applicationContext.publishEvent(new ServerShutdownEvent(this)); + if (serviceInstance != null) { + applicationContext.publishEvent(new ServiceShutdownEvent(serviceInstance)); + } + if (applicationContext.isRunning()) { + applicationContext.stop(); + } + } catch (Throwable e) { + if (LOG.isErrorEnabled()) { + LOG.error("Error stopping Micronaut server: " + e.getMessage(), e); + } + } + } + private NioEventLoopGroup newEventLoopGroup(NettyHttpServerConfiguration.EventLoopConfig config) { if (config != null) { Optional executorService = config.getExecutorName().flatMap(name -> beanLocator.findBean(ExecutorService.class, Qualifiers.byName(name)));