Search in sources :

Example 1 with EventLoopGroupConfiguration

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);
}
Also used : HttpClientException(io.micronaut.http.client.exceptions.HttpClientException) DefaultEventLoopGroupConfiguration(io.micronaut.http.netty.channel.DefaultEventLoopGroupConfiguration) EventLoopGroupConfiguration(io.micronaut.http.netty.channel.EventLoopGroupConfiguration) DefaultEventLoopGroupConfiguration(io.micronaut.http.netty.channel.DefaultEventLoopGroupConfiguration)

Example 2 with 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;
}
Also used : DefaultEventLoopGroupConfiguration(io.micronaut.http.netty.channel.DefaultEventLoopGroupConfiguration) EventLoopGroupConfiguration(io.micronaut.http.netty.channel.EventLoopGroupConfiguration) ArrayList(java.util.ArrayList) Router(io.micronaut.web.router.Router) BindException(java.net.BindException) AsciiString(io.netty.util.AsciiString) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ServerStartupException(io.micronaut.http.server.exceptions.ServerStartupException)

Example 3 with EventLoopGroupConfiguration

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);
        }
    }
}
Also used : ServerShutdownEvent(io.micronaut.runtime.server.event.ServerShutdownEvent) DefaultEventLoopGroupConfiguration(io.micronaut.http.netty.channel.DefaultEventLoopGroupConfiguration) EventLoopGroupConfiguration(io.micronaut.http.netty.channel.EventLoopGroupConfiguration) ServiceStoppedEvent(io.micronaut.discovery.event.ServiceStoppedEvent)

Aggregations

DefaultEventLoopGroupConfiguration (io.micronaut.http.netty.channel.DefaultEventLoopGroupConfiguration)3 EventLoopGroupConfiguration (io.micronaut.http.netty.channel.EventLoopGroupConfiguration)3 ServiceStoppedEvent (io.micronaut.discovery.event.ServiceStoppedEvent)1 HttpClientException (io.micronaut.http.client.exceptions.HttpClientException)1 ServerStartupException (io.micronaut.http.server.exceptions.ServerStartupException)1 ServerShutdownEvent (io.micronaut.runtime.server.event.ServerShutdownEvent)1 Router (io.micronaut.web.router.Router)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 AsciiString (io.netty.util.AsciiString)1 BindException (java.net.BindException)1 ArrayList (java.util.ArrayList)1