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:books.netty.protocol.netty.server.NettyServer.java

License:Apache License

public void bind() throws Exception {
    // ??NIO//from  w ww.j a v a  2 s.c  om
    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, 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());
                }
            });

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

From source file:c5db.control.ControlService.java

License:Apache License

private void startHttpRpc() {
    try {/*ww  w .j a v  a2s  .  c o m*/
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        ServerBootstrap serverBootstrap1 = serverBootstrap.group(acceptConnectionGroup, ioWorkerGroup)
                .channel(NioServerSocketChannel.class).option(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.SO_BACKLOG, 100).childOption(ChannelOption.TCP_NODELAY, true)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();

                        //              pipeline.addLast("logger", new LoggingHandler(LogLevel.DEBUG));
                        pipeline.addLast("http-server", new HttpServerCodec());
                        pipeline.addLast("aggregator",
                                new HttpObjectAggregator(C5ServerConstants.MAX_CALL_SIZE));

                        pipeline.addLast("encode", new ServerHttpProtostuffEncoder());
                        pipeline.addLast("decode", new ServerHttpProtostuffDecoder());

                        pipeline.addLast("translate", new ServerDecodeCommandRequest());

                        pipeline.addLast("inc-messages", new MessageHandler());
                    }
                });

        serverBootstrap.bind(modulePort).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    // yay
                    listenChannel = future.channel();
                    notifyStarted();
                } else {
                    LOG.error("Unable to bind to port {}", modulePort);
                    notifyFailed(future.cause());
                }
            }
        });
    } catch (Exception e) {
        notifyFailed(e);
    }
}

From source file:c5db.replication.ReplicatorService.java

License:Apache License

/**
 * ********** Service startup/registration and shutdown/termination **************
 *///from  w ww.jav a2 s . co  m
@Override
protected void doStart() {
    // must start the fiber up early.
    fiber = fiberSupplier.getNewFiber(this::failModule);
    setupEventChannelSubscription();
    fiber.start();

    C5Futures.addCallback(getDependedOnModules(), (ignore) -> {
        ChannelInitializer<SocketChannel> initer = new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ChannelPipeline p = ch.pipeline();
                p.addLast("frameDecode", new ProtobufVarint32FrameDecoder());
                p.addLast("pbufDecode", new ProtostuffDecoder<>(ReplicationWireMessage.getSchema()));

                p.addLast("frameEncode", new ProtobufVarint32LengthFieldPrepender());
                p.addLast("pbufEncoder", new ProtostuffEncoder<ReplicationWireMessage>());

                p.addLast(new MessageHandler());
            }
        };

        serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_BACKLOG, 100)
                .childOption(ChannelOption.TCP_NODELAY, true).childHandler(initer);

        //noinspection RedundantCast
        serverBootstrap.bind(port).addListener((ChannelFutureListener) future -> {
            if (future.isSuccess()) {
                LOG.info("successfully bound node {} port {} ", nodeId, port);
                listenChannel = future.channel();
            } else {
                LOG.error("Unable to bind! ", future.cause());
                failModule(future.cause());
            }
        });

        outgoingBootstrap.group(workerGroup).channel(NioSocketChannel.class)
                .option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.TCP_NODELAY, true)
                .handler(initer);

        //noinspection Convert2MethodRef
        outgoingRequests.subscribe(fiber, message -> handleOutgoingMessage(message),
                // Clean up cancelled requests.
                message -> handleCancelledSession(message.getSession()));

        notifyStarted();

    }, (Throwable t) -> {
        LOG.error("ReplicatorService unable to retrieve modules!", t);
        failModule(t);
    }, fiber);
}

From source file:cat.tbq.hospital.nio.echoExample.EchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//from  w  ww .  j  av a  2 s .  com
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();

    } else {
        sslCtx = null;

    }
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        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();
                        if (sslCtx != null) {
                            p.addLast(sslCtx.newHandler(ch.alloc()));

                        }
                        //p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoServerHandler());
                    }
                });
        // Start the server.
        ChannelFuture f = b.bind(PORT).sync();
        // Wait until the 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:cc.changic.platform.etl.schedule.http.HttpServer.java

License:Apache License

public void bind() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {// ww w. j  av a  2 s.c om
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new HttpServerInitializer());

        Channel ch = b.bind(port).sync().channel();

        //            System.err.println("Open your web browser and navigate to " +
        //                    (SSL? "https" : "http") + "://127.0.0.1:" + PORT + '/');

        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:cc.ly.mc.core.server.io.SocketServer.java

License:Apache License

public void run() {
    // Configure the server.
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    try {/*from   w w  w  .j  ava2 s  . com*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).option(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.TCP_NODELAY, true)
                .childHandler(new SocketServerInitializer(connectedListeners, disconnectedListeners));
        // Start the server.
        // Wait until the server socket is closed.
        try {
            b.bind(port).sync().channel().closeFuture().sync();
        } catch (InterruptedException e) {
        }
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:cc.ly.mc.server.netty.SocketServer.java

License:Apache License

public void run() {
    // Configure the server.
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    try {/*from w  ww .  ja va2s  .c o  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).option(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.TCP_NODELAY, true).childHandler(new SocketServerInitializer());
        // Start the server.
        // Wait until the server socket is closed.
        try {
            b.bind(port).sync().channel().closeFuture().sync();
        } catch (InterruptedException e) {
        }
    } finally {
        // Shut down all event loops to terminate all threads.
        close();
    }
}

From source file:cc.sharper.netty.TimeServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO SocketChannel
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from w  w  w  .  jav a  2 s  . c o  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler());//io
        // ???
        ChannelFuture f = b.bind(port).sync();

        // ???
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:club.jmint.crossing.server.CrossingServer.java

License:Apache License

public void start() {
    //load configuration
    //ConfigWizard cw = (ConfigWizard)WizardManager.getWizard("ConfigWizard");
    ServerConfig config = (ServerConfig) ConfigWizard.getConfig(Constants.CONFIG_SERVER);
    this.address = config.getItem("server.bind_address");
    this.port = Integer.parseInt(config.getItem("server.port"));
    this.backlog = Integer.parseInt(config.getItem("server.backlog"));
    this.ssl = Boolean.parseBoolean(config.getItem("server.ssl"));

    //Configure SSL
    if (ssl) {//from  w w  w.  j a va 2s  .c  o  m
        try {
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
        } catch (Exception e) {
            CrossLog.printStackTrace(e);
        }
    } else {
        sslCtx = null;
    }

    //Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, backlog).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ServerChannelInitializer(sslCtx));

        // Start the server.
        ChannelFuture f = b.bind(port).sync();
        CrossLog.logger.info("Crossing Server started......");

        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();

    } catch (InterruptedException e) {
        CrossLog.printStackTrace(e);
    } finally {
        // Shut down all event loops to terminate all threads.
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
        CrossLog.logger.info("Crossing Server shutdown......");
    }
}

From source file:cn.david.main.EchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//w  w w .j  a va 2s  .c  o m
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    } else {
        sslCtx = null;
    }

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        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();
                        if (sslCtx != null) {
                            p.addLast(sslCtx.newHandler(ch.alloc()));
                        }
                        //p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoServerHandler());
                    }
                });

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

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