use of io.micronaut.web.router.Router in project micronaut-core by micronaut-projects.
the class NettyHttpServer method start.
@Override
public synchronized NettyEmbeddedServer start() {
if (!isRunning()) {
// suppress unused
// done here to prevent a blocking service loader in the event loop
EventLoopGroupConfiguration workerConfig = resolveWorkerConfiguration();
workerGroup = createWorkerEventLoopGroup(workerConfig);
parentGroup = createParentEventLoopGroup();
ServerBootstrap serverBootstrap = createServerBootstrap();
serverBootstrap.channelFactory(() -> nettyEmbeddedServices.getServerSocketChannelInstance(workerConfig));
processOptions(serverConfiguration.getOptions(), serverBootstrap::option);
processOptions(serverConfiguration.getChildOptions(), serverBootstrap::childOption);
childHandler = new NettyHttpServerInitializer();
serverBootstrap = serverBootstrap.group(parentGroup, workerGroup).childHandler(childHandler);
Optional<String> host = serverConfiguration.getHost();
final String definedHost = host.orElse(null);
serverPort = bindServerToHost(serverBootstrap, definedHost, serverPort);
if (isDefault) {
List<Integer> defaultPorts = new ArrayList<>(2);
defaultPorts.add(serverPort);
if (serverConfiguration.isDualProtocol()) {
defaultPorts.add(bindServerToHost(serverBootstrap, definedHost, getHttpPort(serverConfiguration)));
}
final Router router = this.nettyEmbeddedServices.getRouter();
final Set<Integer> exposedPorts = router.getExposedPorts();
this.boundPorts.addAll(defaultPorts);
if (CollectionUtils.isNotEmpty(exposedPorts)) {
router.applyDefaultPorts(defaultPorts);
for (Integer exposedPort : exposedPorts) {
if (!defaultPorts.contains(exposedPort)) {
try {
if (definedHost != null) {
serverBootstrap.bind(definedHost, exposedPort).sync();
} else {
serverBootstrap.bind(exposedPort).sync();
}
this.boundPorts.add(exposedPort);
} catch (Throwable e) {
final boolean isBindError = e instanceof BindException;
if (LOG.isErrorEnabled()) {
if (isBindError) {
LOG.error("Unable to start server. Additional specified server port {} already in use.", exposedPort);
} else {
LOG.error("Error starting Micronaut server: " + e.getMessage(), e);
}
}
throw new ServerStartupException("Unable to start Micronaut server on port: " + serverPort, e);
}
}
}
}
}
fireStartupEvents();
running.set(true);
}
return this;
}
Aggregations