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.sheldon.javaPrj.netty.DiscardServer.java

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//  w  ww .  j  a va2 s .  c o  m
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new DiscardServerHandler());
                    }

                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);

        ChannelFuture f = b.bind(port).sync();

        f.channel().closeFuture().sync();

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

From source file:com.sheldon.javaPrj.netty.FileServer.java

public void start() {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from  w w w .ja  v a2 s.  c  om
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true)
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new FileServerHandler());
                    }

                });

        ChannelFuture f = b.bind(port).sync();

        System.out.println("server start");

        f.channel().closeFuture().sync();

        System.out.println("server down");

    } catch (InterruptedException ex) {
        Logger.getLogger(FileServer.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        workerGroup.shutdownGracefully();
        bossGroup.shutdownGracefully();
    }

}

From source file:com.sheldon.javaPrj.netty.TimeServer.java

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from  w w w  .  ja  v  a  2s. com*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new TimeServerHandler());
                    }

                }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);

        ChannelFuture f = b.bind(port).sync();

        f.channel().closeFuture().sync();

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

From source file:com.slyak.services.proxy.server.NettyProxyServer.java

License:Apache License

@SneakyThrows(InterruptedException.class)
public void start() {
    ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
    ServerBootstrap bootstrap = new ServerBootstrap();
    bossGroup = new NioEventLoopGroup(proxyProperties.getBoss());
    workerGroup = new NioEventLoopGroup(proxyProperties.getWorker());
    clientGroup = new NioEventLoopGroup(proxyProperties.getClient());
    try {//  w  w  w.  ja v  a 2 s  .  c  o m
        bootstrap.group(bossGroup, workerGroup).channel(getChannelClass())
                .option(ChannelOption.SO_BACKLOG, proxyProperties.getBackLog())
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, proxyProperties.getConnectTimeout())

                .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_REUSEADDR, true)

                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        //channel time out handler
                        pipeline.addLast(new IdleStateHandler(0, 0, 30));
                        pipeline.addLast(new IdleEventHandler());
                        //logging
                        pipeline.addLast(new LoggingHandler());

                        if (isRouter()) {
                            pipeline.addLast(getProxyHandler(proxyProperties));
                        } else {
                            pipeline.addLast(getCustomChannelHandlers(clientGroup));
                        }
                        pipeline.addLast(ExceptionHandler.INSTANCE);
                    }
                });
        //start server
        ChannelFuture future = bootstrap.bind(proxyProperties.getPort()).sync();
        log.debug("Starting proxy server , port is {}", proxyProperties.getPort());
        future.channel().closeFuture().sync();
    } finally {
        stop();
    }
}

From source file:com.smoketurner.packet.application.config.NettyConfiguration.java

License:Apache License

@JsonIgnore
public ChannelFuture build(@Nonnull final Environment environment, @Nonnull final Uploader uploader,
        @Nonnull final Size maxUploadSize) throws SSLException, CertificateException {

    // Configure SSL
    final SslContext sslCtx;
    if (ssl) {//  w ww. j  av a2s. co m
        if (selfSignedCert) {
            final SelfSignedCertificate ssc = new SelfSignedCertificate();
            sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
        } else {
            sslCtx = SslContextBuilder.forServer(new File(keyCertChainFile), new File(keyFile)).build();
        }
    } else {
        sslCtx = null;
    }

    final UploadInitializer initializer = new UploadInitializer(sslCtx, uploader, maxLength.toBytes(),
            maxUploadSize.toBytes());

    final EventLoopGroup bossGroup;
    final EventLoopGroup workerGroup;
    if (Epoll.isAvailable()) {
        LOGGER.info("Using epoll event loop");
        bossGroup = new EpollEventLoopGroup(1);
        workerGroup = new EpollEventLoopGroup();
    } else {
        LOGGER.info("Using NIO event loop");
        bossGroup = new NioEventLoopGroup(1);
        workerGroup = new NioEventLoopGroup();
    }

    environment.lifecycle().manage(new EventLoopGroupManager(bossGroup));
    environment.lifecycle().manage(new EventLoopGroupManager(workerGroup));

    final ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup).handler(new LoggingHandler(LogLevel.INFO))
            .option(ChannelOption.SO_BACKLOG, 100).childOption(ChannelOption.SO_KEEPALIVE, true)
            .childHandler(initializer);

    if (Epoll.isAvailable()) {
        bootstrap.channel(EpollServerSocketChannel.class);
    } else {
        bootstrap.channel(NioServerSocketChannel.class);
    }

    // Start the server
    final ChannelFuture future = bootstrap.bind(listenPort);
    environment.lifecycle().manage(new ChannelFutureManager(future));
    return future;
}

From source file:com.sohail.alam.http.server.SetupServer.java

License:Apache License

public void initialize() {
    final ServerBootstrap serverBootstrap = new ServerBootstrap();
    final EventLoopGroup boss = new NioEventLoopGroup();
    final EventLoopGroup worker = new NioEventLoopGroup();

    serverBootstrap.group(boss, worker).channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_BACKLOG, ServerProperties.PROP.SO_BACKLOG)
            .childOption(ChannelOption.TCP_NODELAY, ServerProperties.PROP.TCP_NODELAY)
            .childOption(ChannelOption.SO_KEEPALIVE, ServerProperties.PROP.SO_KEEPALIVE)
            .childOption(ChannelOption.SO_REUSEADDR, ServerProperties.PROP.SO_REUSEADDR)
            .childHandler(new HttpChannelInitializer());

    try {//from  w ww  . j  a  va2  s .com
        ChannelFuture future = serverBootstrap.bind(new InetSocketAddress(ip, port)).sync();
        if (future.isSuccess()) {
            LoggerManager.LOGGER.info("Http Server Started Successfully @ {}:{}", ip, port);
        } else {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
            LoggerManager.LOGGER.fatal("Http Server Start Failed. Can not bind to {}:{}", ip, port);
        }
    } catch (Exception e) {
        LoggerManager.LOGGER.fatal("Exception Caught while starting Http Server", e);
    }

}

From source file:com.sohu.jafka.http.HttpServer.java

License:Apache License

public void run() {
    // Configure the server.
    ////from www . j  a  v  a  2 s.  co m

    try {
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new HttpServerInitializer(this));

        Channel ch = b.bind(port).sync().channel();
        //System.err.println("Open your web browser and navigate to " + "http://127.0.0.1:" + port + '/');
        logger.info("Jafka HttpServer start at port {}", port);
        ch.closeFuture().sync();
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new RuntimeException(ie.getMessage(), ie);
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
    logger.warn("Jafka HttpServer run over");
}

From source file:com.splicemachine.stream.StreamListenerServer.java

License:Apache License

public void start() throws StandardException {
    ThreadFactory tf = new ThreadFactoryBuilder().setDaemon(true)
            .setNameFormat("StreamerListenerServer-boss-%s").build();
    this.bossGroup = new NioEventLoopGroup(4, tf);
    tf = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("StreamerListenerServer-worker-%s").build();
    this.workerGroup = new NioEventLoopGroup(4, tf);
    try {/* w w  w. ja v  a  2s . co m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new OpenHandler(this))
                .option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);

        // Bind and start to accept incoming connections.
        ChannelFuture f = b.bind(port).sync();

        this.serverChannel = f.channel();
        InetSocketAddress socketAddress = (InetSocketAddress) this.serverChannel.localAddress();
        String host = InetAddress.getLocalHost().getHostName();
        int port = socketAddress.getPort();

        this.hostAndPort = HostAndPort.fromParts(host, port);
        LOG.info("StreamListenerServer listening on " + hostAndPort);

    } catch (IOException e) {
        throw Exceptions.parseException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.spotify.ffwd.protocol.ProtocolServersImpl.java

License:Apache License

private AsyncFuture<ProtocolConnection> bindTCP(final Logger log, final Protocol protocol,
        ProtocolServer server, RetryPolicy policy) {
    final ServerBootstrap b = new ServerBootstrap();

    b.group(boss, worker);//w  w  w .j  av  a2  s .c  om
    b.channel(NioServerSocketChannel.class);
    b.childHandler(server.initializer());

    b.option(ChannelOption.SO_BACKLOG, 128);

    if (protocol.getReceiveBufferSize() != null) {
        b.childOption(ChannelOption.SO_RCVBUF, protocol.getReceiveBufferSize());
    }

    b.childOption(ChannelOption.SO_KEEPALIVE, true);

    final String host = protocol.getAddress().getHostString();
    final int port = protocol.getAddress().getPort();

    final RetryingProtocolConnection connection = new RetryingProtocolConnection(async, timer, log, policy,
            new ProtocolChannelSetup() {
                @Override
                public ChannelFuture setup() {
                    return b.bind(host, port);
                }

                @Override
                public String toString() {
                    return String.format("bind tcp://%s:%d", host, port);
                }
            });

    return connection.getInitialFuture();
}

From source file:com.springapp.mvc.netty.example.spdy.server.SpdyServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
            .applicationProtocolConfig(//ww  w. ja va2  s.  co m
                    new ApplicationProtocolConfig(Protocol.NPN, SelectorFailureBehavior.CHOOSE_MY_LAST_PROTOCOL,
                            SelectedListenerFailureBehavior.CHOOSE_MY_LAST_PROTOCOL,
                            SelectedProtocol.SPDY_3_1.protocolName(), SelectedProtocol.HTTP_1_1.protocolName()))
            .build();

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

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

        System.err
                .println("Open your SPDY-enabled web browser and navigate to https://127.0.0.1:" + PORT + '/');
        System.err.println("If using Chrome browser, check your SPDY sessions at chrome://net-internals/#spdy");

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