Example usage for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup

List of usage examples for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup

Introduction

In this page you can find the example usage for io.netty.channel.nio NioEventLoopGroup NioEventLoopGroup.

Prototype

public NioEventLoopGroup(ThreadFactory threadFactory) 

Source Link

Document

Create a new instance using the default number of threads, the given ThreadFactory and the SelectorProvider which is returned by SelectorProvider#provider() .

Usage

From source file:com.codeabovelab.dm.platform.http.async.NettyRequestFactory.java

License:Apache License

/**
 * Create a new {@code Netty4ClientHttpRequestFactory} with a default
 * {@link NioEventLoopGroup}./*from  w  w w  .  ja v a  2 s  .  com*/
 */
public NettyRequestFactory() {
    int ioWorkerCount = Runtime.getRuntime().availableProcessors() * 2;
    this.eventLoopGroup = new NioEventLoopGroup(ioWorkerCount);
    this.defaultEventLoopGroup = true;
}

From source file:com.codebroker.core.service.NettyNetService.java

License:Open Source License

@Override
public void init(Object object) {
    logger.info("?Netty ");
    PropertiesWrapper propertiesWrapper = (PropertiesWrapper) object;

    int defaultValue = Runtime.getRuntime().availableProcessors() * 2;

    bossGroupNum = propertiesWrapper.getIntProperty(SystemEnvironment.NETTY_BOSS_GROUP_NUM, defaultValue);
    workerGroupNum = propertiesWrapper.getIntProperty(SystemEnvironment.NETTY_WORKER_GROUP_NUM, defaultValue);
    backlog = propertiesWrapper.getIntProperty(SystemEnvironment.NETTY_BACKLOG, BACKLOG);

    name = propertiesWrapper.getProperty(SystemEnvironment.NETTY_SERVER_NAME, "NETTY_SERVER");
    int port = propertiesWrapper.getIntProperty(SystemEnvironment.TCP_PROT, D_PORT);
    Thread thread = new Thread(new Runnable() {
        public void run() {
            bootstrap = new ServerBootstrap();
            bossGroup = new NioEventLoopGroup(bossGroupNum);
            workerGroup = new NioEventLoopGroup(workerGroupNum);
            bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, backlog)
                    .option(ChannelOption.SO_REUSEADDR, Boolean.valueOf(true))
                    // .option(ChannelOption.TCP_NODELAY,
                    // Boolean.valueOf(true))
                    // .option(ChannelOption.SO_KEEPALIVE,
                    // Boolean.valueOf(true))
                    .childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_KEEPALIVE, true)
                    .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                    .childOption(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
                    .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new NettyServerInitializer());
            ChannelFuture f;//  ww w .j av a2s.  c o  m
            try {
                f = bootstrap.bind(port).sync();
                f.channel().closeFuture().sync();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }, "Netty-Start-Thread");
    thread.start();
    logger.info("?Netty ?");
    super.setActive();
}

From source file:com.corundumstudio.socketio.SocketIOServer.java

License:Apache License

protected void initGroups() {
    if (configCopy.isUseLinuxNativeEpoll()) {
        bossGroup = new EpollEventLoopGroup(configCopy.getBossThreads());
        workerGroup = new EpollEventLoopGroup(configCopy.getWorkerThreads());
    } else {/*from   w ww.  j  a  v a 2 s.  co m*/
        bossGroup = new NioEventLoopGroup(configCopy.getBossThreads());
        workerGroup = new NioEventLoopGroup(configCopy.getWorkerThreads());
    }
}

From source file:com.couchbase.client.core.env.CouchbaseEnvironment.java

License:Open Source License

public CouchbaseEnvironment(final Config config, String namespace) {
    this.config = config;
    this.namespace = namespace;

    ioPool = new NioEventLoopGroup(ioPoolSize());
}

From source file:com.creamsugardonut.HttpStaticFileServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    System.out.println("available cores : " + Runtime.getRuntime().availableProcessors() * 2);
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//www .  j  av  a 2s .c o m
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new HttpStaticFileServerInitializer());
        Channel ch = b.bind(PORT).sync().channel();
        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.crystal.chat.SecureChatServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from   w w w  . j a  v a  2  s .  c  o m
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup)

                .channel(NioServerSocketChannel.class).option(ChannelType.op, ChannelType.TYPE_TCP)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new SecureChatServerInitializer(sslCtx));

        ChannelFuture future = b.bind("127.0.0.1", Port.PORT).sync().channel().closeFuture().sync();

    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.ctrip.xpipe.redis.console.health.netty.EchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    final SslContext sslCtx;
    if (SSL) {//  w ww.  j a  v a  2  s .  c  o  m
        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).option(ChannelOption.TCP_NODELAY, true)
                .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:com.demo.netty.echo.EchoServer.java

License:Apache License

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

    //EventLoopGroup is  abstraction of Notification and Thread loop
    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:com.digisky.innerproxy.server.HttpServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.
    ProcessServerManager.getInstance().init();
    final SslContext sslCtx;
    if (SSL) {//from  w w  w.  j  a va2 s.  c  om
        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)
                //.handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new HttpServerInitializer(sslCtx));

        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:com.dingwang.netty.client.PersonClient.java

License:Open Source License

public void run() {
    EventLoopGroup workerGroup = new NioEventLoopGroup(10);

    //BootStrapServerBootstrap,???channel?channel
    Bootstrap b = new Bootstrap();

    //?EventLoopGroup?bossworkder
    //??boss//from  w w  w  .jav a  2  s.c  o m
    b.group(workerGroup);

    //NioServerSocketChannelNioSocketChannel,channel
    b.channel(NioSocketChannel.class);

    //??ServerBootstrap?childOption()SocketChannelchannel
    b.option(ChannelOption.SO_KEEPALIVE, true);
    b.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new PersonEncoder(), new PersonDecoder(), new PersonInHandler());
        }
    });

    ChannelFuture f;
    try {
        //connect()bind()
        f = b.connect(host, port).sync();
        ChannelPool.setChannel(f.channel());
        //            Person p = new Person();
        //            p.setAge(10);
        //            p.setName("dw");
        //            f.channel().writeAndFlush(p);

        //            Thread.currentThread().sleep(1000);
        //
        //            p.setName("test");
        //            f.channel().writeAndFlush(p);
        //            f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        //            workerGroup.shutdownGracefully();
    }
}