Example usage for io.netty.channel EventLoopGroup shutdownGracefully

List of usage examples for io.netty.channel EventLoopGroup shutdownGracefully

Introduction

In this page you can find the example usage for io.netty.channel EventLoopGroup shutdownGracefully.

Prototype

Future<?> shutdownGracefully();

Source Link

Document

Shortcut method for #shutdownGracefully(long,long,TimeUnit) with sensible default values.

Usage

From source file:com.hxr.javatone.concurrency.netty.official.qotm.QuoteOfTheMomentClient.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {//from   w  w  w  . ja va  2s.  com
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true)
                .handler(new QuoteOfTheMomentClientHandler());

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

        // Broadcast the QOTM request to port 8080.
        ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("QOTM?", CharsetUtil.UTF_8),
                new InetSocketAddress("255.255.255.255", port))).sync();

        // QuoteOfTheMomentClientHandler will close the DatagramChannel when a
        // response is received.  If the channel is not closed within 5 seconds,
        // print an error message and quit.
        if (!ch.closeFuture().await(5000)) {
            System.err.println("QOTM request timed out.");
        }
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.hxr.javatone.concurrency.netty.official.qotm.QuoteOfTheMomentServer.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {/*from   www  . ja v  a  2 s.c om*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true)
                .handler(new QuoteOfTheMomentServerHandler());

        b.bind(port).sync().channel().closeFuture().await();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.hxr.javatone.concurrency.netty.official.rxtx.RxtxClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new OioEventLoopGroup();
    try {/*from   www  .j a  va2 s .  c o m*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(RxtxChannel.class).handler(new ChannelInitializer<RxtxChannel>() {
            @Override
            public void initChannel(RxtxChannel ch) throws Exception {
                ch.pipeline().addLast(new LineBasedFrameDecoder(32768), new StringEncoder(),
                        new StringDecoder(), new RxtxClientHandler());
            }
        });

        ChannelFuture f = b.connect(new RxtxDeviceAddress("/dev/ttyUSB0")).sync();

        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.hxr.javatone.concurrency.netty.official.securechat.SecureChatClient.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {/*from w ww  .ja  v a  2  s . com*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new SecureChatClientInitializer());

        // Start the connection attempt.
        Channel ch = b.connect(host, port).sync().channel();

        // Read commands from the stdin.
        ChannelFuture lastWriteFuture = null;
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        for (;;) {
            String line = in.readLine();
            if (line == null) {
                break;
            }

            // Sends the received line to the server.
            lastWriteFuture = ch.writeAndFlush(line + "\r\n");

            // If user typed the 'bye' command, wait until the server closes
            // the connection.
            if ("bye".equals(line.toLowerCase())) {
                ch.closeFuture().sync();
                break;
            }
        }

        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }
    } finally {
        // The connection is closed automatically on shutdown.
        group.shutdownGracefully();
    }
}

From source file:com.hxr.javatone.concurrency.netty.official.securechat.SecureChatServer.java

License:Apache License

public void run() throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from  w w w . j av a 2  s . co  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new SecureChatServerInitializer());

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

From source file:com.hxr.javatone.concurrency.netty.official.telnet.TelnetServer.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/* www.jav a 2 s. c  o  m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new TelnetServerInitializer());

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

From source file:com.hzmsc.scada.Jmtis.server.PortUnificationServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL context
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    final 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  a2 s  .  c o m*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        System.out.println("initChannel..........................");
                        ch.pipeline().addLast(new PortUnificationServerHandler(sslCtx));
                    }
                });

        // Bind and start to accept incoming connections.
        b.bind(PORT).sync().channel().closeFuture().sync();
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.hzq.nio.websocket.WebSocketServer.java

License:Apache License

public void run(int port) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from ww w .  j a  v  a  2  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 {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast("http-codec", new HttpServerCodec());
                        pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
                        ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
                        pipeline.addLast("handler", new WebSocketServerHandler());
                    }
                });

        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.ibm.crail.datanode.netty.server.NettyServer.java

License:Apache License

public void run() {
    /* start the netty server */
    EventLoopGroup acceptGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from www.  j av a 2  s .  c  o m*/
        ServerBootstrap boot = new ServerBootstrap();
        boot.group(acceptGroup, workerGroup);
        /* we use sockets */
        boot.channel(NioServerSocketChannel.class);
        /* for new incoming connection */
        boot.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                LOG.info("TID: " + Thread.currentThread().getId()
                        + " , a new client connection has arrived from : " + ch.remoteAddress().toString());
                /* incoming pipeline */
                ch.pipeline().addLast(new RdmaDecoderRx(), /* this makes full RDMA messages */
                        new IncomingRequestHandler(ch, dataNode));
                /* outgoing pipeline */
                //ch.pipeline().addLast(new RdmaEncoderTx());
            }
        });
        /* general optimization settings */
        boot.option(ChannelOption.SO_BACKLOG, 1024);
        boot.childOption(ChannelOption.SO_KEEPALIVE, true);

        /* now we bind the server and start */
        ChannelFuture f = boot.bind(this.inetSocketAddress.getAddress(), this.inetSocketAddress.getPort())
                .sync();
        LOG.info("Datanode binded to : " + this.inetSocketAddress);
        /* at this point we are binded and ready */
        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
        acceptGroup.shutdownGracefully();
        LOG.info("Datanode at " + this.inetSocketAddress + " is shutdown");
    }
}

From source file:com.ibm.crail.namenode.rpc.netty.NettyNameNode.java

License:Apache License

public void run(final RpcNameNodeService service) {
    /* here we run the incoming RPC service */
    InetSocketAddress inetSocketAddress = CrailUtils.getNameNodeAddress();
    LOG.info("Starting the NettyNamenode service at : " + inetSocketAddress);
    /* start the netty server */
    EventLoopGroup acceptGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*  w w w . j av  a  2s  . c o m*/
        ServerBootstrap boot = new ServerBootstrap();
        boot.group(acceptGroup, workerGroup);
        /* we use sockets */
        boot.channel(NioServerSocketChannel.class);
        /* for new incoming connection */
        boot.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel ch) throws Exception {
                LOG.info("A new connection has arrived from : " + ch.remoteAddress().toString());
                /* incoming pipeline */
                ch.pipeline().addLast("RequestDecoder", new RequestDecoder());
                ch.pipeline().addLast("NNProcessor", new NamenodeProcessor(service));
                /* outgoing pipeline */
                ch.pipeline().addLast("ResponseEncoder", new ResponseEncoder());
            }
        });
        /* general optimization settings */
        boot.option(ChannelOption.SO_BACKLOG, 1024);
        boot.childOption(ChannelOption.SO_KEEPALIVE, true);

        /* now we bind the server and start */
        ChannelFuture f = boot.bind(inetSocketAddress.getAddress(), inetSocketAddress.getPort()).sync();
        /* at this point we are binded and ready */
        f.channel().closeFuture().sync();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
        acceptGroup.shutdownGracefully();
        LOG.info("Netty namenode at " + inetSocketAddress + " is shutdown");
    }
}