Search in sources :

Example 36 with NioServerSocketChannelFactory

use of org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory in project canal by alibaba.

the class CanalAdminWithNetty method start.

public void start() {
    super.start();
    this.bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
    /*
         * enable keep-alive mechanism, handle abnormal network connection
         * scenarios on OS level. the threshold parameters are depended on OS.
         * e.g. On Linux: net.ipv4.tcp_keepalive_time = 300
         * net.ipv4.tcp_keepalive_probes = 2 net.ipv4.tcp_keepalive_intvl = 30
         */
    bootstrap.setOption("child.keepAlive", true);
    /*
         * optional parameter.
         */
    bootstrap.setOption("child.tcpNoDelay", true);
    // 构造对应的pipeline
    bootstrap.setPipelineFactory(() -> {
        ChannelPipeline pipelines = Channels.pipeline();
        pipelines.addLast(FixedHeaderFrameDecoder.class.getName(), new FixedHeaderFrameDecoder());
        // support to maintain child socket channel.
        pipelines.addLast(HandshakeInitializationHandler.class.getName(), new HandshakeInitializationHandler(childGroups));
        pipelines.addLast(ClientAuthenticationHandler.class.getName(), new ClientAuthenticationHandler(canalAdmin));
        SessionHandler sessionHandler = new SessionHandler(canalAdmin);
        pipelines.addLast(SessionHandler.class.getName(), sessionHandler);
        return pipelines;
    });
    // 启动
    if (StringUtils.isNotEmpty(ip)) {
        this.serverChannel = bootstrap.bind(new InetSocketAddress(this.ip, this.port));
    } else {
        this.serverChannel = bootstrap.bind(new InetSocketAddress(this.port));
    }
}
Also used : SessionHandler(com.alibaba.otter.canal.admin.handler.SessionHandler) ClientAuthenticationHandler(com.alibaba.otter.canal.admin.handler.ClientAuthenticationHandler) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) HandshakeInitializationHandler(com.alibaba.otter.canal.admin.handler.HandshakeInitializationHandler) InetSocketAddress(java.net.InetSocketAddress) FixedHeaderFrameDecoder(com.alibaba.otter.canal.server.netty.handler.FixedHeaderFrameDecoder) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Example 37 with NioServerSocketChannelFactory

use of org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory in project http-client by biasedbit.

the class DummyHttpServer method init.

// interface ------------------------------------------------------------------------------------------------------
public boolean init() {
    Executor bossExecutor = Executors.newCachedThreadPool();
    Executor workerExecutor = Executors.newCachedThreadPool();
    ChannelFactory factory;
    if (useOldIo)
        factory = new OioServerSocketChannelFactory(bossExecutor, workerExecutor);
    else
        factory = new NioServerSocketChannelFactory(bossExecutor, workerExecutor);
    bootstrap = new ServerBootstrap(factory);
    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();
            if (useSsl) {
                SSLEngine engine = BogusSslContextFactory.getInstance().getServerContext().createSSLEngine();
                engine.setUseClientMode(false);
                pipeline.addLast("ssl", new SslHandler(engine));
            }
            pipeline.addLast("encoder", new HttpResponseEncoder());
            pipeline.addLast("decoder", new HttpRequestDecoder());
            if (compressionLevel > 0)
                pipeline.addLast("compressor", new HttpContentCompressor(compressionLevel));
            // 5MB
            pipeline.addLast("aggregator", new HttpChunkAggregator(5242880));
            pipeline.addLast("handler", new RequestHandler());
            return pipeline;
        }
    });
    channelGroup = new DefaultChannelGroup("hotpotato-dummy-server-" + Integer.toHexString(hashCode()));
    SocketAddress bindAddress = (host != null) ? new InetSocketAddress(host, port) : new InetSocketAddress(port);
    Channel serverChannel = bootstrap.bind(bindAddress);
    channelGroup.add(serverChannel);
    return (running = serverChannel.isBound());
}
Also used : DefaultChannelGroup(org.jboss.netty.channel.group.DefaultChannelGroup) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) SSLEngine(javax.net.ssl.SSLEngine) InetSocketAddress(java.net.InetSocketAddress) OioServerSocketChannelFactory(org.jboss.netty.channel.socket.oio.OioServerSocketChannelFactory) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) SslHandler(org.jboss.netty.handler.ssl.SslHandler) Executor(java.util.concurrent.Executor) OioServerSocketChannelFactory(org.jboss.netty.channel.socket.oio.OioServerSocketChannelFactory) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 38 with NioServerSocketChannelFactory

use of org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory in project http-client by biasedbit.

the class UploadMirrorHttpServer method init.

// interface ------------------------------------------------------------------------------------------------------
public boolean init() {
    Executor bossExecutor = Executors.newCachedThreadPool();
    Executor workerExecutor = Executors.newCachedThreadPool();
    ChannelFactory factory = new NioServerSocketChannelFactory(bossExecutor, workerExecutor);
    bootstrap = new ServerBootstrap(factory);
    bootstrap.setOption("child.tcpNoDelay", true);
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = Channels.pipeline();
            if (useSsl) {
                SSLEngine engine = BogusSslContextFactory.getInstance().getServerContext().createSSLEngine();
                engine.setUseClientMode(false);
                pipeline.addLast("ssl", new SslHandler(engine));
            }
            pipeline.addLast("codec", new HttpServerCodec());
            pipeline.addLast("handler", new RequestHandler());
            return pipeline;
        }
    });
    channelGroup = new DefaultChannelGroup("hotpotato-upload-server-" + Integer.toHexString(hashCode()));
    SocketAddress bindAddress = (host != null) ? new InetSocketAddress(host, port) : new InetSocketAddress(port);
    Channel serverChannel = bootstrap.bind(bindAddress);
    channelGroup.add(serverChannel);
    return (running = serverChannel.isBound());
}
Also used : DefaultChannelGroup(org.jboss.netty.channel.group.DefaultChannelGroup) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) SSLEngine(javax.net.ssl.SSLEngine) InetSocketAddress(java.net.InetSocketAddress) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) SslHandler(org.jboss.netty.handler.ssl.SslHandler) Executor(java.util.concurrent.Executor) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Example 39 with NioServerSocketChannelFactory

use of org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory in project pinpoint by naver.

the class PinpointServerAcceptor method createBootStrap.

private ServerBootstrap createBootStrap(int bossCount, int workerCount) {
    // profiler, collector
    ExecutorService boss = Executors.newCachedThreadPool(new PinpointThreadFactory("Pinpoint-Server-Boss", true));
    NioServerBossPool nioServerBossPool = new NioServerBossPool(boss, bossCount, ThreadNameDeterminer.CURRENT);
    ExecutorService worker = Executors.newCachedThreadPool(new PinpointThreadFactory("Pinpoint-Server-Worker", true));
    NioWorkerPool nioWorkerPool = new NioWorkerPool(worker, workerCount, ThreadNameDeterminer.CURRENT);
    NioServerSocketChannelFactory nioClientSocketChannelFactory = new NioServerSocketChannelFactory(nioServerBossPool, nioWorkerPool);
    return new ServerBootstrap(nioClientSocketChannelFactory);
}
Also used : NioServerBossPool(org.jboss.netty.channel.socket.nio.NioServerBossPool) NioWorkerPool(org.jboss.netty.channel.socket.nio.NioWorkerPool) NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) ExecutorService(java.util.concurrent.ExecutorService) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) PinpointThreadFactory(com.navercorp.pinpoint.common.profiler.concurrent.PinpointThreadFactory)

Example 40 with NioServerSocketChannelFactory

use of org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory in project NabAlive by jcheype.

the class NabaliveServer method start.

@PostConstruct
public void start() {
    logger.info("Starting server.");
    // Configure the server.
    bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
    // Set up the pipeline factory.
    bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        public ChannelPipeline getPipeline() throws Exception {
            ChannelPipeline pipeline = pipeline();
            pipeline.addLast("timeout", new IdleStateHandler(timer, 0, 0, 20));
            pipeline.addLast("nabaliveServerHandler", nabaliveServerHandler);
            return pipeline;
        }
    });
    bootstrap.setOption("reuseAddress", true);
    // Bind and start to accept incoming connections.
    bind = bootstrap.bind(new InetSocketAddress(XMPP_PORT));
}
Also used : NioServerSocketChannelFactory(org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory) InetSocketAddress(java.net.InetSocketAddress) IdleStateHandler(org.jboss.netty.handler.timeout.IdleStateHandler) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory) ServerBootstrap(org.jboss.netty.bootstrap.ServerBootstrap) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) PostConstruct(javax.annotation.PostConstruct)

Aggregations

NioServerSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory)41 ServerBootstrap (org.jboss.netty.bootstrap.ServerBootstrap)39 InetSocketAddress (java.net.InetSocketAddress)30 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)16 ChannelPipelineFactory (org.jboss.netty.channel.ChannelPipelineFactory)12 ChannelFactory (org.jboss.netty.channel.ChannelFactory)9 ExecutorService (java.util.concurrent.ExecutorService)6 Channel (org.jboss.netty.channel.Channel)6 Executor (java.util.concurrent.Executor)5 DefaultChannelGroup (org.jboss.netty.channel.group.DefaultChannelGroup)5 IOException (java.io.IOException)4 NioClientSocketChannelFactory (org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory)4 NioServerBossPool (org.jboss.netty.channel.socket.nio.NioServerBossPool)4 NioWorkerPool (org.jboss.netty.channel.socket.nio.NioWorkerPool)4 InetAddress (java.net.InetAddress)3 OioServerSocketChannelFactory (org.jboss.netty.channel.socket.oio.OioServerSocketChannelFactory)3 HttpRequestDecoder (org.jboss.netty.handler.codec.http.HttpRequestDecoder)3 HttpResponseEncoder (org.jboss.netty.handler.codec.http.HttpResponseEncoder)3 Bean (org.springframework.context.annotation.Bean)3 FixedHeaderFrameDecoder (com.alibaba.otter.canal.server.netty.handler.FixedHeaderFrameDecoder)2