use of io.micronaut.http.netty.channel.EventLoopGroupConfiguration in project micronaut-core by micronaut-projects.
the class DefaultNettyHttpClientRegistry method resolveSocketChannelFactory.
private ChannelFactory resolveSocketChannelFactory(HttpClientConfiguration configuration, BeanContext beanContext) {
final String eventLoopGroup = configuration.getEventLoopGroup();
final EventLoopGroupConfiguration eventLoopGroupConfiguration = beanContext.findBean(EventLoopGroupConfiguration.class, Qualifiers.byName(eventLoopGroup)).orElseGet(() -> {
if (EventLoopGroupConfiguration.DEFAULT.equals(eventLoopGroup)) {
return new DefaultEventLoopGroupConfiguration();
} else {
throw new HttpClientException("Specified event loop group is not defined: " + eventLoopGroup);
}
});
return () -> eventLoopGroupFactory.clientSocketChannelInstance(eventLoopGroupConfiguration);
}
use of io.micronaut.http.netty.channel.EventLoopGroupConfiguration 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;
}
use of io.micronaut.http.netty.channel.EventLoopGroupConfiguration in project micronaut-core by micronaut-projects.
the class NettyHttpServer method stopInternal.
private void stopInternal() {
try {
if (shutdownParent) {
EventLoopGroupConfiguration parent = serverConfiguration.getParent();
if (parent != null) {
long quietPeriod = parent.getShutdownQuietPeriod().toMillis();
long timeout = parent.getShutdownTimeout().toMillis();
parentGroup.shutdownGracefully(quietPeriod, timeout, TimeUnit.MILLISECONDS).addListener(this::logShutdownErrorIfNecessary);
} else {
parentGroup.shutdownGracefully().addListener(this::logShutdownErrorIfNecessary);
}
}
if (shutdownWorker) {
workerGroup.shutdownGracefully().addListener(this::logShutdownErrorIfNecessary);
}
webSocketSessions.close();
applicationContext.getEventPublisher(ServerShutdownEvent.class).publishEvent(new ServerShutdownEvent(this));
if (serviceInstance != null) {
applicationContext.getEventPublisher(ServiceStoppedEvent.class).publishEvent(new ServiceStoppedEvent(serviceInstance));
}
if (isDefault) {
if (applicationContext.isRunning()) {
applicationContext.stop();
}
serverConfiguration.getMultipart().getLocation().ifPresent(dir -> DiskFileUpload.baseDirectory = null);
}
serverConfiguration.getMultipart().getLocation().ifPresent(dir -> DiskFileUpload.baseDirectory = null);
childHandler = null;
this.boundPorts.clear();
} catch (Throwable e) {
if (LOG.isErrorEnabled()) {
LOG.error("Error stopping Micronaut server: " + e.getMessage(), e);
}
}
}
Aggregations