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.study.hc.net.netty.EchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure the server.
    // EventLoopGroup   accept NioEventLoop
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    // EventLoopGroup   I/O
    EventLoopGroup workerGroup2 = new NioEventLoopGroup(1);
    try {/*from   w ww.j a va 2s  .  c  om*/
        // ??
        ServerBootstrap b = new ServerBootstrap();
        // ???reactor???
        b.group(bossGroup, workerGroup2).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.DEBUG))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        p.addLast(new EchoServerHandler());
                    }
                });
        // bind??
        ChannelFuture f = b.bind(PORT).sync();
        // ??
        f.channel().closeFuture().sync();
    } finally {
        // 
        bossGroup.shutdownGracefully();
        workerGroup2.shutdownGracefully();
    }
}

From source file:com.sunan.van.client.TestVanClient.java

License:Apache License

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

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

        register(DEFAULT_TOPIC, ch);
        // 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 {
        group.shutdownGracefully();
    }
}

From source file:com.system.distribute.server.FileServer.java

License:Apache License

public void run() throws Exception {
    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from   w ww.j av a  2 s.  c o  m
        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 {
                        ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8),
                                new LineBasedFrameDecoder(8192), new StringDecoder(CharsetUtil.UTF_8),
                                new FileHandler());
                    }
                });

        // 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.tam.server_babicare.Client.java

public void run() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {//from  w w w  .j  a va  2 s. c  o  m
        Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSctpChannel.class)
                .handler(new ClientInitializer());
        Channel channel = bootstrap.connect(host, port).sync().channel();
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            channel.write(in.readLine() + "\r\n");
        }
    } catch (Exception e) {
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.tam.server_babicare.Server.java

public void run() throws InterruptedException {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from  w w w. ja  v a  2  s .  c  o m*/
        ServerBootstrap bootstrap = new ServerBootstrap().group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class).childHandler(new ServerInitializer());
        bootstrap.bind(port).sync().channel().closeFuture().sync();

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

From source file:com.tch.test.chat.netty.NettyClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {//from  w  w w  .  j  a v a2s  .  com
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new MyClientChannelInitializer());

        // 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.tch.test.chat.netty.NettyServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from   w  w  w.ja va 2s .  c o m
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new MyServerChannelInitializer());

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

From source file:com.tcy.app.netty4.Ne4Client.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup group = new NioEventLoopGroup();
    try {/* w ww .j  a  v a 2  s  . co m*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new Ne4ClientHandler(firstMessageSize));

        // Make the connection attempt.
        ChannelFuture f = b.connect(host, port).sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.tesora.dve.standalone.LoadBalancer.java

License:Open Source License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(0, new DefaultThreadFactory("lb-boss"));
    EventLoopGroup workerGroup = new NioEventLoopGroup(0, new DefaultThreadFactory("lb-worker"));

    ServerBootstrap b = new ServerBootstrap();

    try {//  w  w  w  . ja va2 s.  c  o  m
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(proxyInitializer)
                .childOption(ChannelOption.AUTO_READ, false).bind(lbPort).sync().channel().closeFuture().sync();
    } catch (Throwable e) {
        throw new Exception("Failed to start load balancer on port " + lbPort, e);
    } finally {
        //         b.shutdown();
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
        proxyInitializer.close();
    }
}

From source file:com.test.zp.netty.EchoClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure SSL.git
    //        final SslContext sslCtx;
    //        if (SSL) {
    //            sslCtx = SslContextBuilder.forClient()
    //                .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
    //        } else {
    //            sslCtx = null;
    //        }//from  w w  w  .  j a  va2  s  . co m

    // Configure the client.
    EventLoopGroup group = new NioEventLoopGroup();
    try {
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();
                        //                     if (sslCtx != null) {
                        //                         p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
                        //                     }
                        //p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoClientHandler());
                    }
                });

        // Start the client.
        ChannelFuture f = b.connect(HOST, PORT).sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        group.shutdownGracefully();
    }
}