Example usage for io.netty.channel ChannelOption SO_BACKLOG

List of usage examples for io.netty.channel ChannelOption SO_BACKLOG

Introduction

In this page you can find the example usage for io.netty.channel ChannelOption SO_BACKLOG.

Prototype

ChannelOption SO_BACKLOG

To view the source code for io.netty.channel ChannelOption SO_BACKLOG.

Click Source Link

Usage

From source file:com.zextras.modules.chat.server.xmpp.netty.ChatXmppService.java

License:Open Source License

private ServerBootstrap buildBoostrap(EventLoopGroup acceptorGroup, EventLoopGroup workerGroup,
        final SSLContext zimbraSSLContext, final boolean oldSSL) {

    ServerBootstrap serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(acceptorGroup, workerGroup);

    serverBootstrap.channel(NioServerSocketChannel.class);

    ChannelHandler handler = new ChannelInitializer<SocketChannel>() {
        @Override/*from   ww  w  .jav  a 2  s  .  com*/
        public void initChannel(SocketChannel ch) throws Exception {
            try {
                if (oldSSL) {
                    final SSLEngine engine = zimbraSSLContext.createSSLEngine();
                    engine.setUseClientMode(false);
                    ch.pipeline().addFirst(null, "SSL", new SslHandler(engine, false));
                }

                ch.pipeline().addLast(null, "SubTagTokenizer", new XmlSubTagTokenizer());
                FirstTags firstTagsHandler = new FirstTags(mXmppHandlerFactory, mEventManager, ch,
                        mSchemaProvider, zimbraSSLContext, oldSSL, mChatProperties, mNettyService,
                        mProxyAuthRequestEncoder, mXmppEventFilter, mXmppFilterOut);
                ch.pipeline().addAfter("SubTagTokenizer", "FirstTags", firstTagsHandler);
            } catch (Throwable ex) {
                ChatLog.log.warn("Unable to initialize XMPP connection: " + Utils.exceptionToString(ex));
                ch.close();
            }
        }
    };

    serverBootstrap.childHandler(handler).option(ChannelOption.SO_BACKLOG, 128)
            .childOption(ChannelOption.SO_KEEPALIVE, true).childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 0);

    return serverBootstrap;
}

From source file:com.zextras.modules.chat.services.LocalXmppService.java

License:Open Source License

@Override
public void run() {
    ChatLog.log.info("Listening on port " + DEFAULT_LOCAL_XMPP_PORT);
    EventLoopGroup acceptorGroup = new NioEventLoopGroup(4);
    EventLoopGroup channelWorkerGroup = new NioEventLoopGroup(8);

    Channel channel;/*from  w  w  w.java2s.c  o m*/
    try {
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(acceptorGroup, channelWorkerGroup);
        bootstrap.channel(NioServerSocketChannel.class);
        ChannelHandler handler = new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                try {
                    SSLEngine sslEngine = mZimbraSSLContextProvider.get().createSSLEngine();
                    sslEngine.setUseClientMode(false);
                    SslHandler sslHandler = new SslHandler(sslEngine);
                    ch.pipeline().addFirst("ssl", sslHandler);
                    ch.pipeline().addLast(null, "SubTagTokenizer", new XmlSubTagTokenizer());
                    ch.pipeline().addLast(null, "XmlTagTokenizer", new XmlTagTokenizer());
                    ch.pipeline().addAfter("XmlTagTokenizer", "StanzaProcessor",
                            new ChannelInboundHandlerAdapter() {
                                @Override
                                public void channelRead(ChannelHandlerContext ctx, Object msg) {
                                    mLocalXmppReceiver.processStanza((String) msg);
                                }
                            });
                } catch (Throwable t) {
                    ChatLog.log.warn("Unable to initializer XMPP connection: " + Utils.exceptionToString(t));
                    ch.close();
                }
            }
        };

        ChannelFuture channelFuture = bootstrap.childHandler(handler).option(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 0).bind(DEFAULT_LOCAL_XMPP_PORT).sync();

        if (!channelFuture.isSuccess()) {
            throw channelFuture.cause();
        }

        channel = channelFuture.channel();
        mInitializationPromise.setSuccess(null);
    } catch (Throwable e) {
        mInitializationPromise.setFailure(e);
        return;
    }

    mLock.lock();
    try {
        while (!mStopRequested) {
            try {
                mWaitStopRequest.await();
            } catch (InterruptedException ignored) {
            }
        }

        channel.close().sync();

        acceptorGroup.shutdownGracefully().sync();
        channelWorkerGroup.shutdownGracefully().sync();
    } catch (InterruptedException ignored) {
    } finally {
        mLock.unlock();
    }
}

From source file:com.zhucode.longio.transport.netty.NettyConnector.java

License:Open Source License

private void runOneHttpServer(int port, Dispatcher dispatcher, ProtocolType pt) {
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup);//from www  .  ja va  2s.  c o m
    b.channel(NioServerSocketChannel.class);

    b.childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new HttpServerCodec());
            ch.pipeline().addLast(new HttpObjectAggregator(65536));
            ch.pipeline().addLast(new IdleStateHandler(6000, 3000, 0));
            ch.pipeline().addLast(new HttpHandler(NettyConnector.this, dispatcher, callbackDispatcher,
                    getProtocolParser(pt)));
        }

    });
    b.option(ChannelOption.SO_BACKLOG, 4096);

    b.childOption(ChannelOption.SO_KEEPALIVE, true);

    ChannelFuture f;
    try {
        f = b.bind(port).sync();
        f.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.zhucode.longio.transport.netty.NettyConnector.java

License:Open Source License

private void runOneRawSocketServer(int port, Dispatcher dispatcher, ProtocolType pt) {
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup);/*  w ww .  j a v  a 2 s .  co  m*/
    b.channel(NioServerSocketChannel.class);

    b.childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast("decoder", new LengthFieldBasedFrameDecoder(65536, 0, 2, 0, 2));
            ch.pipeline().addLast("encoder", new LengthFieldPrepender(2, false));
            ch.pipeline().addLast(new IdleStateHandler(6000, 3000, 0));
            ch.pipeline().addLast(new RawSocketHandler(NettyConnector.this, dispatcher, callbackDispatcher,
                    getProtocolParser(pt)));
        }

    });
    b.option(ChannelOption.SO_BACKLOG, 4096);

    b.childOption(ChannelOption.SO_KEEPALIVE, true);

    ChannelFuture f;
    try {
        f = b.bind(port).sync();
        f.channel().closeFuture().sync();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.ztesoft.zsmart.zmq.remoting.netty.NettyRemotingServer.java

License:Apache License

@Override
public void start() {
    this.defaultEventExecutorGroup = new DefaultEventExecutorGroup(//
            nettyServerConfig.getServerWorkThreads(), //
            new ThreadFactory() {

                private AtomicInteger threadIndex = new AtomicInteger(0);

                @Override//from  ww w . j a  va  2s  .  c  o  m
                public Thread newThread(Runnable r) {
                    return new Thread(r, "NettyServerWorkerThread_" + this.threadIndex.incrementAndGet());
                }
            });

    ServerBootstrap childHandler = //
            this.serverBootstrap.group(this.eventLoopGroupBoss, this.eventLoopGroupWorker)
                    .channel(NioServerSocketChannel.class)
                    //
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    //
                    .option(ChannelOption.SO_REUSEADDR, true)
                    //
                    .option(ChannelOption.SO_KEEPALIVE, false)
                    //
                    .childOption(ChannelOption.TCP_NODELAY, true)
                    //
                    .option(ChannelOption.SO_SNDBUF, nettyServerConfig.getServerSocketSndBufSize())
                    //
                    .option(ChannelOption.SO_RCVBUF, nettyServerConfig.getServerSocketRcvBufSize())
                    //
                    .localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort()))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(
                                    //
                                    defaultEventExecutorGroup, //
                                    new NettyEncoder(), //
                                    new NettyDecoder(), //
                                    new IdleStateHandler(0, 0,
                                            nettyServerConfig.getServerChannelMaxIdleTimeSeconds()), //
                                    new NettyConnetManageHandler(), //
                                    new NettyServerHandler());
                        }
                    });

    if (nettyServerConfig.isServerPooledByteBufAllocatorEnable()) {
        // ????
        childHandler.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    }

    try {
        ChannelFuture sync = this.serverBootstrap.bind().sync();
        InetSocketAddress addr = (InetSocketAddress) sync.channel().localAddress();
        this.port = addr.getPort();
    } catch (InterruptedException e1) {
        throw new RuntimeException("this.serverBootstrap.bind().sync() InterruptedException", e1);
    }

    if (this.channelEventListener != null) {
        this.nettyEventExecuter.start();
    }

    // ?1??
    this.timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            try {
                NettyRemotingServer.this.scanResponseTable();
            } catch (Exception e) {
                log.error("scanResponseTable exception", e);
            }
        }
    }, 1000 * 3, 1000);
}

From source file:com.zz.learning.netty5.chap12.server.NettyServer.java

License:Apache License

private void bind() throws Exception {
    // ??NIO/*from w  ww . j  a  va 2 s .  c o m*/
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100)
            .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws IOException {
                    ch.pipeline().addLast(new NettyMessageDecoder(1024 * 1024));
                    // ch.pipeline().addLast(new NettyMessageDecoder(1024 *
                    // 1024, 4, 4));
                    ch.pipeline().addLast(new NettyMessageEncoder());
                    ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50));
                    ch.pipeline().addLast(new LoginAuthRespHandler());
                    ch.pipeline().addLast("HeartBeatHandler", new HeartBeatRespHandler());
                    ch.pipeline().addLast("BusinessMessageRespHandler", new BusinessMessageRespHandler());
                }
            });

    // ???
    ChannelFuture cf = b.bind(NettyConstant.REMOTEIP, NettyConstant.PORT).sync();
    //  serverChannel = cf.channel();
    System.out.println("Netty server start ok : " + (NettyConstant.REMOTEIP + " : " + NettyConstant.PORT));
}

From source file:controlspy3.ControlSpy3.java

public void run() {

    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try {/*  ww w  . j  av  a  2  s . com*/
        // Server
        ServerBootstrap boots = new ServerBootstrap();
        boots.group(bossGroup, workerGroup);
        boots.channel(NioServerSocketChannel.class);
        boots.childHandler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new TimeEncoder(), new SpyAsServer());
                if (ALGO == false) {
                    System.out.println("se salio ");
                }
                System.out.println("Cliente conectado!");
            }
        }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);

        //SERVER
        // Bind and start to accept incoming connections.
        ChannelFuture future_ch = boots.bind(port).sync();

        //SERVER
        // Wait until the server socket is closed. Server
        future_ch.channel().closeFuture().sync();

    } catch (InterruptedException ex) {

    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }
}

From source file:core.communication.threadpool.io.CallServer.java

License:Apache License

public static void connectServer() throws Exception {
    // Configure the bootstrap.server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    final CallServerHandler serverHandler = new CallServerHandler();
    try {/*w  ww  .j  ava  2s  . com*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(serverHandler);
                    }
                });

        // Start the bootstrap.server.
        ChannelFuture f = b.bind(PORT).sync();

        // Wait until the bootstrap.server socket is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:core.HttpServer.java

License:Apache License

public void run() throws Exception {
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(8);
    try {// w w w .j  a v a2  s  . com
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new HttpServerInitializer());

        b.bind(port).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:darks.grid.network.GridMessageServer.java

License:Apache License

@Override
public boolean initialize() {
    try {//from   w  w w .java 2  s  .co  m
        NetworkConfig config = GridRuntime.config().getNetworkConfig();
        int bossNum = Runtime.getRuntime().availableProcessors() * config.getServerBossThreadDelta();
        int workerNum = config.getServerWorkerThreadNumber();
        bossGroup = new NioEventLoopGroup(bossNum);
        workerGroup = new NioEventLoopGroup(workerNum);
        super.initialize();
        bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY, config.isTcpNodelay())
                .option(ChannelOption.SO_KEEPALIVE, config.isTcpKeepAlive())
                .option(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true))
                //            .option(ChannelOption.SO_TIMEOUT, config.getRecvTimeout())
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout())
                .option(ChannelOption.SO_REUSEADDR, config.isTcpReuseAddr())
                //            .option(ChannelOption.SO_BACKLOG, config.getTcpBacklog())
                .option(ChannelOption.SO_SNDBUF, config.getTcpSendBufferSize())
                .option(ChannelOption.SO_RCVBUF, config.getTcpRecvBufferSize())
                .childOption(ChannelOption.TCP_NODELAY, config.isTcpNodelay())
                .childOption(ChannelOption.SO_KEEPALIVE, config.isTcpKeepAlive())
                .childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true))
                .childOption(ChannelOption.SO_TIMEOUT, config.getRecvTimeout())
                .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeout())
                .childOption(ChannelOption.SO_REUSEADDR, config.isTcpReuseAddr())
                .childOption(ChannelOption.SO_BACKLOG, config.getTcpBacklog())
                .childOption(ChannelOption.SO_SNDBUF, config.getTcpSendBufferSize())
                .childOption(ChannelOption.SO_RCVBUF, config.getTcpRecvBufferSize());
        bootstrap.childHandler(newChannelHandler());
        return true;
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        return false;
    }
}