Search in sources :

Example 21 with ServerBootstrap

use of org.jboss.netty.bootstrap.ServerBootstrap in project cdap by caskdata.

the class NettyRouter method bootstrapServer.

private void bootstrapServer(final ChannelUpstreamHandler connectionTracker) throws ServiceBindException {
    ExecutorService serverBossExecutor = createExecutorService(serverBossThreadPoolSize, "router-server-boss-thread-%d");
    ExecutorService serverWorkerExecutor = createExecutorService(serverWorkerThreadPoolSize, "router-server-worker-thread-%d");
    serverBootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(serverBossExecutor, serverWorkerExecutor));
    serverBootstrap.setOption("backlog", serverConnectionBacklog);
    serverBootstrap.setOption("child.bufferFactory", new DirectChannelBufferFactory());
    // Setup the pipeline factory
    serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();
            if (isSSLEnabled()) {
                // Add SSLHandler is SSL is enabled
                pipeline.addLast("ssl", sslHandlerFactory.create());
            }
            pipeline.addLast("tracker", connectionTracker);
            pipeline.addLast("http-response-encoder", new HttpResponseEncoder());
            pipeline.addLast("http-decoder", new HttpRequestDecoder());
            pipeline.addLast("http-status-request-handler", new HttpStatusRequestHandler());
            if (securityEnabled) {
                pipeline.addLast("access-token-authenticator", new SecurityAuthenticationHttpHandler(realm, tokenValidator, configuration, accessTokenTransformer, discoveryServiceClient));
            }
            // for now there's only one hardcoded rule, but if there will be more, we may want it generic and configurable
            pipeline.addLast("http-request-handler", new HttpRequestHandler(clientBootstrap, serviceLookup, ImmutableList.<ProxyRule>of()));
            return pipeline;
        }
    });
    // Start listening on ports.
    ImmutableMap.Builder<Integer, String> serviceMapBuilder = ImmutableMap.builder();
    for (Map.Entry<String, Integer> forward : serviceToPortMap.entrySet()) {
        int port = forward.getValue();
        String service = forward.getKey();
        String boundService = serviceLookup.getService(port);
        if (boundService != null) {
            LOG.warn("Port {} is already configured to service {}, ignoring forward for service {}", port, boundService, service);
            continue;
        }
        InetSocketAddress bindAddress = new InetSocketAddress(hostname, port);
        LOG.info("Starting Netty Router for service {} on address {}...", service, bindAddress);
        try {
            Channel channel = serverBootstrap.bind(bindAddress);
            InetSocketAddress boundAddress = (InetSocketAddress) channel.getLocalAddress();
            serviceMapBuilder.put(boundAddress.getPort(), service);
            channelGroup.add(channel);
            // Update service map
            serviceLookup.updateServiceMap(serviceMapBuilder.build());
            LOG.info("Started Netty Router for service {} on address {}.", service, boundAddress);
        } catch (ChannelException e) {
            if ((Throwables.getRootCause(e) instanceof BindException)) {
                throw new ServiceBindException("Router", hostname.getCanonicalHostName(), port, e);
            }
            throw e;
        }
    }
}
Also used : ServiceBindException(co.cask.cdap.common.ServiceBindException) HttpRequestHandler(co.cask.cdap.gateway.router.handlers.HttpRequestHandler) InetSocketAddress(java.net.InetSocketAddress) SecurityAuthenticationHttpHandler(co.cask.cdap.gateway.router.handlers.SecurityAuthenticationHttpHandler) HttpStatusRequestHandler(co.cask.cdap.gateway.router.handlers.HttpStatusRequestHandler) HttpRequestDecoder(org.jboss.netty.handler.codec.http.HttpRequestDecoder) ChannelException(org.jboss.netty.channel.ChannelException) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) Channel(org.jboss.netty.channel.Channel) BindException(java.net.BindException) ServiceBindException(co.cask.cdap.common.ServiceBindException) DirectChannelBufferFactory(org.jboss.netty.buffer.DirectChannelBufferFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelException(org.jboss.netty.channel.ChannelException) BindException(java.net.BindException) ServiceBindException(co.cask.cdap.common.ServiceBindException) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) ImmutableMap(com.google.common.collect.ImmutableMap) HttpResponseEncoder(org.jboss.netty.handler.codec.http.HttpResponseEncoder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 22 with ServerBootstrap

use of org.jboss.netty.bootstrap.ServerBootstrap in project opennms by OpenNMS.

the class TcpOutputStrategyTest method setUpClass.

@BeforeClass
public static void setUpClass() {
    // Setup a quick Netty TCP server that decodes the protobuf messages
    // and appends these to a list when received
    ChannelFactory factory = new NioServerSocketChannelFactory();
    ServerBootstrap bootstrap = new ServerBootstrap(factory);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        public ChannelPipeline getPipeline() {
            return Channels.pipeline(new ProtobufDecoder(PerformanceDataReadings.getDefaultInstance()), new PerfDataServerHandler());
        }
    });
    Channel channel = bootstrap.bind(new InetSocketAddress(0));
    InetSocketAddress addr = (InetSocketAddress) channel.getLocalAddress();
    // Point the TCP exporter to our server
    System.setProperty("org.opennms.rrd.tcp.host", addr.getHostString());
    System.setProperty("org.opennms.rrd.tcp.port", Integer.toString(addr.getPort()));
    // Always use queueing during these tests
    System.setProperty("org.opennms.rrd.usequeue", Boolean.TRUE.toString());
    // Use the temporary folder as the base directory
    System.setProperty("rrd.base.dir", tempFolder.getRoot().getAbsolutePath());
}
Also used : NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) ProtobufDecoder(org.jboss.netty.handler.codec.protobuf.ProtobufDecoder) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) ChannelFactory(org.jboss.netty.channel.ChannelFactory) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) BeforeClass(org.junit.BeforeClass)

Example 23 with ServerBootstrap

use of org.jboss.netty.bootstrap.ServerBootstrap in project dubbo by alibaba.

the class NettyServer method doOpen.

@Override
protected void doOpen() throws Throwable {
    NettyHelper.setNettyLoggerFactory();
    ExecutorService boss = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerBoss", true));
    ExecutorService worker = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerWorker", true));
    ChannelFactory channelFactory = new NioServerSocketChannelFactory(boss, worker, getUrl().getPositiveParameter(Constants.IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS));
    bootstrap = new ServerBootstrap(channelFactory);
    final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
    channels = nettyHandler.getChannels();
    // https://issues.jboss.org/browse/NETTY-365
    // https://issues.jboss.org/browse/NETTY-379
    // final Timer timer = new HashedWheelTimer(new NamedThreadFactory("NettyIdleTimer", true));
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        public ChannelPipeline getPipeline() {
            NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyServer.this);
            ChannelPipeline pipeline = Channels.pipeline();
            /*int idleTimeout = getIdleTimeout();
                if (idleTimeout > 10000) {
                    pipeline.addLast("timer", new IdleStateHandler(timer, idleTimeout / 1000, 0, 0));
                }*/
            pipeline.addLast("decoder", adapter.getDecoder());
            pipeline.addLast("encoder", adapter.getEncoder());
            pipeline.addLast("handler", nettyHandler);
            return pipeline;
        }
    });
    // bind
    channel = bootstrap.bind(getBindAddress());
}
Also used : NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) NamedThreadFactory(com.alibaba.dubbo.common.utils.NamedThreadFactory) ExecutorService(java.util.concurrent.ExecutorService) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) ChannelFactory(org.jboss.netty.channel.ChannelFactory) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Example 24 with ServerBootstrap

use of org.jboss.netty.bootstrap.ServerBootstrap in project bagheera by mozilla-metrics.

the class Bagheera method startServer.

/**
 * Start a Bagheera server with the provided settings.
 * Throws if the server could not be started.
 * The caller is responsible for closing the returned instance, and the
 * channel factory if desired.
 */
public static BagheeraServerState startServer(final int port, final boolean tcpNoDelay, final WildcardProperties props, final Producer producer, final NioServerSocketChannelFactory channelFactory, final String channelGroupName, final MetricsManager manager) throws Exception {
    prepareHealthChecks();
    // HTTP server setup.
    final ChannelGroup channelGroup = new DefaultChannelGroup(channelGroupName);
    final ServerBootstrap server = new ServerBootstrap(channelFactory);
    final HttpServerPipelineFactory pipeFactory = new HttpServerPipelineFactory(props, producer, channelGroup, manager);
    server.setPipelineFactory(pipeFactory);
    server.setOption("tcpNoDelay", tcpNoDelay);
    // Disable keep-alive so client connections don't hang around.
    server.setOption("keepAlive", false);
    final Channel channel = server.bind(new InetSocketAddress(port));
    return new BagheeraServerState(port, producer, channelFactory, channel, channelGroup);
}
Also used : DefaultChannelGroup(org.jboss.netty.channel.group.DefaultChannelGroup) InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) DefaultChannelGroup(org.jboss.netty.channel.group.DefaultChannelGroup) ChannelGroup(org.jboss.netty.channel.group.ChannelGroup) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap)

Example 25 with ServerBootstrap

use of org.jboss.netty.bootstrap.ServerBootstrap in project Protocol-Adapter-OSLP by OSGP.

the class ApplicationContext method serverBootstrap.

@Bean(destroyMethod = "releaseExternalResources")
public ServerBootstrap serverBootstrap() {
    final ChannelFactory factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
    final ServerBootstrap bootstrap = new ServerBootstrap(factory);
    bootstrap.setPipelineFactory(() -> {
        final ChannelPipeline pipeline = ApplicationContext.this.createPipeLine();
        LOGGER.info("Created new server pipeline");
        return pipeline;
    });
    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setOption("child.keepAlive", false);
    bootstrap.bind(new InetSocketAddress(this.oslpPortServer()));
    return bootstrap;
}
Also used : NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) InetSocketAddress(java.net.InetSocketAddress) NioClientSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) ChannelFactory(org.jboss.netty.channel.ChannelFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Aggregations

ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)94 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)58 TrackerServer (org.traccar.TrackerServer)42 InetSocketAddress (java.net.InetSocketAddress)38 NioServerSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory)34 ConnectionlessBootstrap (org.jboss.netty.bootstrap.ConnectionlessBootstrap)25 StringEncoder (org.jboss.netty.handler.codec.string.StringEncoder)16 Channel (org.jboss.netty.channel.Channel)13 ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)12 StringDecoder (org.jboss.netty.handler.codec.string.StringDecoder)12 LengthFieldBasedFrameDecoder (org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder)11 ChannelFactory (org.jboss.netty.channel.ChannelFactory)8 ExecutorService (java.util.concurrent.ExecutorService)5 DefaultChannelGroup (org.jboss.netty.channel.group.DefaultChannelGroup)5 Test (org.junit.Test)5 HostnamePort (org.neo4j.helpers.HostnamePort)5 CharacterDelimiterFrameDecoder (org.traccar.CharacterDelimiterFrameDecoder)5 IOException (java.io.IOException)4 ChannelException (org.jboss.netty.channel.ChannelException)4 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)4