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.saturn.http.jmx.ShutdownThread.java

License:Open Source License

/**
 * jmx?Sytem.exit(0),jvm/*from   ww  w .j  a v  a 2s. c o  m*/
 */
@Override
public void run() {
    StringBuilder sb = new StringBuilder();
    sb.append(String.format("??...? : %s\r\n", new Date()));
    long begin = System.currentTimeMillis();
    for (EventLoopGroup eventLoopGroup : _thread._eventLoopGroups) {
        try {
            if (!eventLoopGroup.isShutdown()) {
                eventLoopGroup.shutdownGracefully();
            }
        } catch (Exception ex) {
            LOG.debug("", ex);
        }
    }
    sb.append(String.format("??,... %d ms", System.currentTimeMillis() - begin));
    System.out.println(sb.toString());
    if (isSystemExit()) {
        System.exit(0);
    }
}

From source file:com.seed.nettyechoserver.EchoServer.java

public static void main(String[] args) throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup(1);
    try {/*from   ww w.j a  va  2 s.  co  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 {
                        ChannelPipeline p = ch.pipeline();
                        //p.addLast(new LoggingHandler(LogLevel.INFO));
                        p.addLast(new EchoServerHandler());
                    }
                });

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

From source file:com.seventh_root.ld33.client.LD33Client.java

License:Apache License

public void connect(String address, int port) {
    EventLoopGroup group = new NioEventLoopGroup();
    try {//from   w w  w  . ja va 2 s  .  c om
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            public void initChannel(SocketChannel channel) throws Exception {
                channel.pipeline().addLast(new ObjectEncoder(),
                        new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
                        //new LD33ServerBoundPacketEncoder(),
                        //new LD33ClientBoundPacketDecoder(LD33Client.this),
                        new LD33ClientHandler(LD33Client.this));
            }
        });
        channel = bootstrap.connect(address, port).sync().channel();
        start();
        channel.closeFuture().sync();
    } catch (InterruptedException exception) {
        getLogger().log(SEVERE, "Event loop group interrupted", exception);
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.seventh_root.ld33.server.LD33Server.java

License:Apache License

private void start() {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from  w  w w . j  a v  a2  s .c o m*/
        ServerBootstrap bootstrap = new ServerBootstrap();
        handler = new LD33ServerHandler(this);
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel channel) throws Exception {
                        channel.pipeline().addLast(new ObjectEncoder(),
                                new ObjectDecoder(ClassResolvers.cacheDisabled(null)),
                                //new LD33ClientBoundPacketEncoder(),
                                //new LD33ServerBoundPacketDecoder(LD33Server.this),
                                handler);
                    }
                });
        Channel channel = bootstrap.bind(getConfig().getInt("port", 37896)).sync().channel();
        setRunning(true);
        long beforeTime, timeDiff, sleep;
        beforeTime = currentTimeMillis();
        while (isRunning()) {
            doTick();
            timeDiff = currentTimeMillis() - beforeTime;
            sleep = DELAY - timeDiff;
            if (sleep > 0) {
                try {
                    Thread.sleep(sleep);
                } catch (InterruptedException exception) {
                    getLogger().log(SEVERE, "Thread interrupted", exception);
                }
            }
            beforeTime = currentTimeMillis();
        }
        channel.closeFuture().sync();
    } catch (InterruptedException exception) {
        getLogger().log(SEVERE, "Event loop group interrupted", exception);
    } finally {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.shbxs.netty.SecureChatServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    //SelfSignedCertificate????
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    //???/*from  w ww .  j  ava 2 s.c o  m*/
    SslContext sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
    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 SecureChatServerInitializer(sslCtx));

        b.bind(PORT).sync().channel().closeFuture().sync();
        //bind?channnel
        //sync?future future??future
        //channel futureio??channel
        //closefuture ?????future
        //

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

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

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from ww  w.  j  a v a 2  s.c  om*/
        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.FileClient.java

public void start() {
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*from  www .j  av a2  s  .c om*/
        Bootstrap b = new Bootstrap();
        b.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new ChunkedWriteHandler(), new FileClientHandler());
                    }

                });

        ChannelFuture f = b.connect("localhost", port).sync();

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

    } catch (InterruptedException ex) {
        Logger.getLogger(FileClient.class.getName()).log(Level.SEVERE, null, ex);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
    }

}

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

public void start() {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/* w w  w.  j  a v  a 2 s . c  o m*/
        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.TimeClient.java

public static void main(String args[]) throws InterruptedException {
    String host = args.length > 0 ? args[0] : "localhost";
    int port = Integer.parseInt(args.length > 1 ? args[1] : "8080");

    EventLoopGroup workerGroup = new NioEventLoopGroup();

    try {//from www  . j av  a 2  s  . c  om
        Bootstrap b = new Bootstrap();
        b.group(workerGroup);
        b.channel(NioSocketChannel.class);
        b.option(ChannelOption.SO_KEEPALIVE, true);
        b.handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(new TimeClientHandler());
            }
        });

        ChannelFuture f = b.connect(host, port).sync();

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

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

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//w  ww .  j  av  a 2 s  .c om
        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();
    }
}