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() 

Source Link

Document

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

Usage

From source file:com.antsdb.saltedfish.server.SaltedFish.java

License:Open Source License

/**
 * starting netty/*  ww  w  .j  a v a2 s  . c  o  m*/
 * 
 * @param port
 * @throws InterruptedException 
 */
void startNetty() throws Exception {
    bossGroup = new NioEventLoopGroup();
    workerGroup = new NioEventLoopGroup(this.configService.getNettyWorkerThreadPoolSize());
    _log.info("netty worker pool size: {}", workerGroup.executorCount());

    ServerBootstrap b = new ServerBootstrap();
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
            .childHandler(new MysqlChannelInitializer(this)).option(ChannelOption.SO_BACKLOG, 128)
            .childOption(ChannelOption.SO_KEEPALIVE, true);

    // Bind and start to accept incoming connections.

    _log.info("starting netty on port: " + this.configService.getPort());
    this.f = b.bind(this.configService.getPort()).sync();
}

From source file:com.athena.dolly.websocket.client.test.WebSocketClient.java

License:Apache License

private void initialize() throws InterruptedException {
    group = new NioEventLoopGroup();

    Bootstrap bootstrap = new Bootstrap();
    String protocol = uri.getScheme();
    if (!"ws".equals(protocol)) {
        throw new IllegalArgumentException("Unsupported protocol: " + protocol);
    }/*from   w  w w. j ava  2 s.  c om*/

    HttpHeaders customHeaders = new DefaultHttpHeaders();
    customHeaders.add("MyHeader", "MyValue");

    // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.
    // If you change it to V00, ping is not supported and remember to change
    // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.
    final WebSocketClientHandler handler = new WebSocketClientHandler(WebSocketClientHandshakerFactory
            .newHandshaker(uri, WebSocketVersion.V13, null, false, customHeaders));

    bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast("http-codec", new HttpClientCodec());
            pipeline.addLast("aggregator", new HttpObjectAggregator(8192));
            pipeline.addLast("ws-handler", handler);
        }
    });

    System.out.println("WebSocket Client connecting");
    ch = bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel();
    handler.handshakeFuture().sync();

}

From source file:com.athena.dolly.websocket.server.test.WebSocketServer.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//w w w .  j  a  v a2 s  .  co  m
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new WebSocketServerInitializer());

        Channel ch = b.bind(port).sync().channel();
        System.out.println("Web socket server started at port " + port + '.');
        System.out.println("Open your browser and navigate to http://localhost:" + port + '/');

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

From source file:com.athena.dolly.websocket.server.test.WebSocketSslServer.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/* ww w.ja va2 s .co  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new WebSocketSslServerInitializer());

        Channel ch = b.bind(port).sync().channel();
        System.out.println("Web socket server started at port " + port + '.');
        System.out.println("Open your browser and navigate to https://localhost:" + port + '/');
        ch.closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.baidu.jprotobuf.pbrpc.management.HttpServer.java

License:Apache License

/**
 * /*from w  ww  . j a v  a2 s.co m*/
 */
public HttpServer(RpcServer rpcServer) {
    this.rpcServer = rpcServer;
    bossGroup = new NioEventLoopGroup();
    workerGroup = new NioEventLoopGroup();
}

From source file:com.baidu.jprotobuf.pbrpc.transport.RpcClient.java

License:Apache License

public RpcClient(Class<? extends Channel> clientChannelClass, RpcClientOptions rpcClientOptions) {
    this.workerGroup = new NioEventLoopGroup();
    this.group(workerGroup);
    this.channel(clientChannelClass);
    this.handler(new RpcClientPipelineinitializer(this));
    this.rpcClientOptions = rpcClientOptions;
    this.option(ChannelOption.SO_REUSEADDR, rpcClientOptions.isReuseAddress());
    this.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, rpcClientOptions.getConnectTimeout());
    this.option(ChannelOption.SO_SNDBUF, rpcClientOptions.getSendBufferSize());
    this.option(ChannelOption.SO_RCVBUF, rpcClientOptions.getSendBufferSize());
    this.option(ChannelOption.SO_KEEPALIVE, rpcClientOptions.isKeepAlive());
    this.option(ChannelOption.TCP_NODELAY, rpcClientOptions.getTcpNoDelay());
    this.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR,
            new DefaultMessageSizeEstimator(rpcClientOptions.getReceiveBufferSize()));

    // add count//  w w  w.  java  2s .c o m
    INSTANCE_COUNT.incrementAndGet();
}

From source file:com.baidu.rigel.biplatform.ma.file.client.monitor.FileServerMonitor.java

License:Open Source License

/**
 * ?//from   ww  w.  j  a v  a 2  s .c om
 */
public void start() throws Exception {
    EventLoopGroup work = new NioEventLoopGroup();
    Bootstrap strap = new Bootstrap();
    try {
        strap.group(work).option(ChannelOption.TCP_NODELAY, true).channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {

                    @Override
                    protected void initChannel(NioSocketChannel chl) throws Exception {
                        // ??
                        chl.pipeline().addLast(new ObjectDecoder(1024,
                                ClassResolvers.cacheDisabled(FileServerMonitor.class.getClassLoader())));
                        chl.pipeline().addLast(new ObjectEncoder());
                        chl.pipeline().addLast(new FileServerMonitor());
                    }

                });

        ChannelFuture future = strap.connect(new InetSocketAddress(host, port));
        future.channel().closeFuture().sync();
    } finally {
        executor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(3);
                    ChannelFuture future = strap.connect("localhost", 9090).sync();
                    future.channel().closeFuture().sync();
                } catch (InterruptedException e) {
                    logger.error(e.getMessage(), e);
                }
            }

        });
    }
}

From source file:com.baidu.rigel.biplatform.tesseract.node.service.IndexAndSearchClient.java

License:Open Source License

public IndexAndSearchClient() {
    // channelMaps = new ConcurrentHashMap<NodeAddress, Channel>();
    actionHandlerMaps = new ConcurrentHashMap<String, ChannelHandler>();
    b = new Bootstrap();
    group = new NioEventLoopGroup();
    b.group(group);/*from www .  j av  a2 s . c o  m*/
    b.channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true);
    b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);
    b.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast("encode", new ObjectEncoder());
            pipeline.addLast("decode", new ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(null)));
            //                pipeline.addLast("frameencoder",new LengthFieldPrepender(4,false));
            //                pipeline.addLast("framedecoder",new LengthFieldBasedFrameDecoder(1024*1024*1024, 0, 4,0,4));
        }
    });

    logger.info("IndexAndSearchClient init finished");
}

From source file:com.bala.learning.learning.netty.HttpServer.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  om
        SelfSignedCertificate ssc = new SelfSignedCertificate();
        sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    } else {
        sslCtx = null;
    }

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    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.baoxue.netty.file.FileServer.java

License:Apache License

public void run(int port) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*ww w  . j  av a 2  s. co  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).childHandler(new ChannelInitializer<SocketChannel>() {
                    /*
                     * (non-Javadoc)
                     * 
                     * @see
                     * io.netty.channel.ChannelInitializer#initChannel(io
                     * .netty.channel.Channel)
                     */
                    public void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8),
                                new LineBasedFrameDecoder(1024), new StringDecoder(CharsetUtil.UTF_8),
                                new FileServerHandler());
                    }
                });
        ChannelFuture f = b.bind(port).sync();
        System.out.println("Start file server at port : " + port);
        f.channel().closeFuture().sync();
    } finally {
        // ?
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}