Skip to content

Commit

Permalink
Allow Netty to attempt to bind. Fixes #1081
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed Jan 8, 2019
1 parent b622b78 commit 6bbce91
Showing 1 changed file with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> applicationName = serverConfiguration.getApplicationConfiguration().getName();
if (applicationName.isPresent()) {
if (LOG.isDebugEnabled()) {
Expand All @@ -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);
Expand All @@ -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);
}
}
}
Expand All @@ -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> executorService = config.getExecutorName().flatMap(name -> beanLocator.findBean(ExecutorService.class, Qualifiers.byName(name)));
Expand Down

0 comments on commit 6bbce91

Please sign in to comment.