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:app.WebSocketServer.java

License:Apache License

public static void main(String[] args) throws Exception {

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//from   ww  w  . j  a  v a2  s .c  o  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("Listening on ws://0.0.0.0:" + PORT + "/ws");

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

From source file:appium.android.server.http.HttpServer.java

License:Apache License

public void start() {
    if (serverThread != null) {
        throw new IllegalStateException("Server is already running");
    }//w w  w .  j  a  v a 2  s. c  o m
    serverThread = new Thread() {
        @Override
        public void run() {
            EventLoopGroup bossGroup = new NioEventLoopGroup(1);
            EventLoopGroup workerGroup = new NioEventLoopGroup();
            try {
                ServerBootstrap bootstrap = new ServerBootstrap();
                bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
                bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                        .childHandler(new ServerInitializer(handlers));

                Channel ch = bootstrap.bind(port).sync().channel();
                ch.closeFuture().sync();
            } catch (InterruptedException ignored) {
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
                TrafficCounter.shutdown();
            }
        }
    };
    serverThread.start();
}

From source file:at.yawk.accordion.netty.NettyConnector.java

License:Mozilla Public License

@Override
public Optional<Connection> connect(SocketAddress address) {
    // TODO find a non-hacky method to close channels
    Collection<Channel> registeredChannels = Collections.synchronizedList(new ArrayList<>());

    EventLoopGroup workerGroup = new NioEventLoopGroup() {
        @Override/* ww w  .ja  va 2s  .  co  m*/
        public ChannelFuture register(Channel channel, ChannelPromise promise) {
            registeredChannels.add(channel);
            return super.register(channel, promise);
        }
    };

    AtomicReference<Connection> connectionRef = new AtomicReference<>();

    // init
    Bootstrap bootstrap = new Bootstrap().group(workerGroup).handler(new ChannelHandlerAdapter() {
        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            NettyConnection connection = new NettyConnection(ctx.channel());
            connectionRef.set(connection);
            connection.init();
        }
    }).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true);

    try {
        // connect
        ChannelFuture connectFuture = bootstrap.connect(address);
        // wait for connection
        connectFuture.sync();
        return Optional.of(connectionRef.get());
    } catch (Exception e) {
        // shut down workers
        workerGroup.shutdownGracefully();

        // kill channels
        registeredChannels.forEach(NettyConnection::close);

        return Optional.empty();
    }
}

From source file:baseFrame.netty.atest.HttpHelloWorldServer.java

License:Apache License

public static void main(String[] args) throws Exception {

    // Configure the server.
    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {//  w w w  .j a v a2s .  c  om
        ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new HttpHelloWorldServerInitializer());

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

        System.err.println("Open your web browser and navigate to " + ("http") + "://127.0.0.1:" + PORT + '/');

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

From source file:basic.TimeClient.java

License:Apache License

public void connect(int port, String host) throws Exception {
    // ?NIO//  w  w w .j  a v a 2s . c  om
    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 {
                        ch.pipeline().addLast(new TimeClientHandler());
                    }
                });

        // ??
        ChannelFuture f = b.connect(host, port).sync();
        // 
        f.channel().closeFuture().sync();
    } finally {
        // NIO
        group.shutdownGracefully();
    }
}

From source file:basic.TimeServer.java

License:Apache License

public void bind(int port) throws Exception {
    // ??NIO//  ww  w . ja v a2  s . c  o m
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler());
        // ???
        ChannelFuture f = b.bind(port).sync();

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

From source file:be.mil.ChatServer.netty.server.TCPServer.java

License:Apache License

public void start() {

    EventLoopGroup producer = new NioEventLoopGroup();
    EventLoopGroup consumer = new NioEventLoopGroup();

    try {/*from   ww  w  .  j  av a 2 s .  c o  m*/
        Bootstrap bootstrapClient = new Bootstrap().group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class).handler(new ClientAdapterInitializer());

        ServerBootstrap bootstrap = new ServerBootstrap().group(producer, consumer)
                .channel(NioServerSocketChannel.class).childHandler(serverAdapterInitializer);
        System.out.println("Server started");
        bootstrap.bind(port).sync().channel().closeFuture().sync();
        try {
            channel = bootstrapClient.connect(server, port).sync().channel();

            try {

                channel.write("Hi\n");
                channel.write("Hi\n");
                channel.flush();

            } catch (Exception e) {
                e.printStackTrace();
            } finally {

                bootstrap.group().shutdownGracefully();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        producer.shutdownGracefully();
        consumer.shutdownGracefully();
    }

}

From source file:bean.lee.demo.netty.learn.http.file.HttpStaticFileServer.java

License:Apache License

public void run() throws Exception {
    EventLoopGroup bossGroup = new NioEventLoopGroup();
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    try {/*ww w  .j  a va  2 s . com*/
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new HttpStaticFileServerInitializer());

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

From source file:bean.lee.demo.netty.learn.http.helloworld.HttpHelloWorldServer.java

License:Apache License

public void run() throws Exception {
    // Configure the server.
    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.option(ChannelOption.SO_BACKLOG, 1024);
        b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new HttpHelloWorldServerInitializer());

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

From source file:book.netty.n1basicC3.TimeClient.java

License:Apache License

public void connect(int port, String host) throws Exception {
    // ?NIO/*from   w  ww .  j a  v  a  2  s  .  c o m*/
    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 {
                        ch.pipeline().addLast(new TimeClientHandler());
                    }
                });

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

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