Example usage for io.netty.channel.nio NioEventLoopGroup shutdownGracefully

List of usage examples for io.netty.channel.nio NioEventLoopGroup shutdownGracefully

Introduction

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

Prototype

@Override
    public Future<?> shutdownGracefully() 

Source Link

Usage

From source file:com.doctor.netty5.example.http.helloworld.HelloWorldServer.java

License:Apache License

public void start() throws InterruptedException {
    ServerBootstrap bootstrap = new ServerBootstrap();
    NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
    NioEventLoopGroup workerGroup = new NioEventLoopGroup();

    try {/*ww w. j  a  v a 2s .c o m*/
        bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).localAddress(port)
                .option(ChannelOption.SO_BACKLOG, 1024).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {

                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new HttpServerCodec());
                        ch.pipeline().addLast(new HelloWorldServerHandler());
                    }
                });

        ChannelFuture channelFuture = bootstrap.bind().sync();
        System.out.println(HelloWorldServer.class.getName() + " started and listen on port:"
                + channelFuture.channel().localAddress());

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

From source file:com.flysoloing.learning.network.netty.udt.echo.bytes.ByteEchoClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure the client.
    final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
    final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory,
            NioUdtProvider.BYTE_PROVIDER);
    try {/*from  w ww  .j  a  v a2 s  .c  o  m*/
        final Bootstrap boot = new Bootstrap();
        boot.group(connectGroup).channelFactory(NioUdtProvider.BYTE_CONNECTOR)
                .handler(new ChannelInitializer<UdtChannel>() {
                    @Override
                    public void initChannel(final UdtChannel ch) throws Exception {
                        ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new ByteEchoClientHandler());
                    }
                });
        // Start the client.
        final ChannelFuture f = boot.connect(HOST, PORT).sync();
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        connectGroup.shutdownGracefully();
    }
}

From source file:com.flysoloing.learning.network.netty.udt.echo.bytes.ByteEchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    final ThreadFactory acceptFactory = new DefaultThreadFactory("accept");
    final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
    final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1, acceptFactory, NioUdtProvider.BYTE_PROVIDER);
    final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory,
            NioUdtProvider.BYTE_PROVIDER);

    // Configure the server.
    try {/*from ww  w.j a v a 2s. co  m*/
        final ServerBootstrap boot = new ServerBootstrap();
        boot.group(acceptGroup, connectGroup).channelFactory(NioUdtProvider.BYTE_ACCEPTOR)
                .option(ChannelOption.SO_BACKLOG, 10).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<UdtChannel>() {
                    @Override
                    public void initChannel(final UdtChannel ch) throws Exception {
                        ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new ByteEchoServerHandler());
                    }
                });
        // Start the server.
        final ChannelFuture future = boot.bind(PORT).sync();
        // Wait until the server socket is closed.
        future.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        acceptGroup.shutdownGracefully();
        connectGroup.shutdownGracefully();
    }
}

From source file:com.flysoloing.learning.network.netty.udt.echo.message.MsgEchoClient.java

License:Apache License

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

    // Configure the client.
    final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
    final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory,
            NioUdtProvider.MESSAGE_PROVIDER);
    try {/*  ww w .  j  a  va 2 s.  co  m*/
        final Bootstrap boot = new Bootstrap();
        boot.group(connectGroup).channelFactory(NioUdtProvider.MESSAGE_CONNECTOR)
                .handler(new ChannelInitializer<UdtChannel>() {
                    @Override
                    public void initChannel(final UdtChannel ch) throws Exception {
                        ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new MsgEchoClientHandler());
                    }
                });
        // Start the client.
        final ChannelFuture f = boot.connect(HOST, PORT).sync();
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        connectGroup.shutdownGracefully();
    }
}

From source file:com.flysoloing.learning.network.netty.udt.echo.message.MsgEchoServer.java

License:Apache License

public static void main(String[] args) throws Exception {
    final ThreadFactory acceptFactory = new DefaultThreadFactory("accept");
    final ThreadFactory connectFactory = new DefaultThreadFactory("connect");
    final NioEventLoopGroup acceptGroup = new NioEventLoopGroup(1, acceptFactory,
            NioUdtProvider.MESSAGE_PROVIDER);
    final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory,
            NioUdtProvider.MESSAGE_PROVIDER);

    // Configure the server.
    try {//w w  w  .  j ava  2  s  .c o  m
        final ServerBootstrap boot = new ServerBootstrap();
        boot.group(acceptGroup, connectGroup).channelFactory(NioUdtProvider.MESSAGE_ACCEPTOR)
                .option(ChannelOption.SO_BACKLOG, 10).handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<UdtChannel>() {
                    @Override
                    public void initChannel(final UdtChannel ch) throws Exception {
                        ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO), new MsgEchoServerHandler());
                    }
                });
        // Start the server.
        final ChannelFuture future = boot.bind(PORT).sync();
        // Wait until the server socket is closed.
        future.channel().closeFuture().sync();
    } finally {
        // Shut down all event loops to terminate all threads.
        acceptGroup.shutdownGracefully();
        connectGroup.shutdownGracefully();
    }
}

From source file:com.flysoloing.learning.network.netty.udt.echo.rendezvous.MsgEchoPeerBase.java

License:Apache License

public void run() throws Exception {
    // Configure the peer.
    final ThreadFactory connectFactory = new DefaultThreadFactory("rendezvous");
    final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory,
            NioUdtProvider.MESSAGE_PROVIDER);
    try {/*from w w  w.j  av  a2  s  . co m*/
        final Bootstrap boot = new Bootstrap();
        boot.group(connectGroup).channelFactory(NioUdtProvider.MESSAGE_RENDEZVOUS)
                .handler(new ChannelInitializer<UdtChannel>() {
                    @Override
                    public void initChannel(final UdtChannel ch) throws Exception {
                        ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO),
                                new MsgEchoPeerHandler(messageSize));
                    }
                });
        // Start the peer.
        final ChannelFuture f = boot.connect(peer, self).sync();
        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
    } finally {
        // Shut down the event loop to terminate all threads.
        connectGroup.shutdownGracefully();
    }
}

From source file:com.flysoloing.learning.network.netty.udt.echo.rendezvousBytes.ByteEchoPeerBase.java

License:Apache License

public void run() throws Exception {
    final ThreadFactory connectFactory = new DefaultThreadFactory("rendezvous");
    final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory,
            NioUdtProvider.BYTE_PROVIDER);
    try {//  ww  w .  ja v  a 2  s  .c  om
        final Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(connectGroup).channelFactory(NioUdtProvider.BYTE_RENDEZVOUS)
                .handler(new ChannelInitializer<UdtChannel>() {
                    @Override
                    protected void initChannel(UdtChannel ch) throws Exception {
                        ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO),
                                new ByteEchoPeerHandler(messageSize));
                    }
                });
        final ChannelFuture future = bootstrap.connect(peerAddress, myAddress).sync();
        future.channel().closeFuture().sync();
    } finally {
        connectGroup.shutdownGracefully();
    }
}

From source file:com.kixeye.kixmpp.p2p.node.NodeServerTest.java

License:Apache License

@Test(expected = java.net.BindException.class)
public void portAlreadyInUseTest() {
    final MessageRegistry messageRegistry = new MessageRegistry();
    final NioEventLoopGroup bossGroup = new NioEventLoopGroup();
    final NioEventLoopGroup workerGroup = new NioEventLoopGroup();
    NodeServer serverA = new NodeServer();
    NodeServer serverB = new NodeServer();
    try {/*from w  ww . j  a  v a 2  s . c  o m*/
        serverA.initialize("127.0.0.1", 8042, bossGroup, workerGroup, messageRegistry,
                new ChannelInboundHandlerAdapter());
        serverB.initialize("127.0.0.1", 8042, bossGroup, workerGroup, messageRegistry,
                new ChannelInboundHandlerAdapter());
    } finally {
        serverA.shutdown();
        serverB.shutdown();
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

From source file:com.kixeye.kixmpp.p2p.node.NodeServerTest.java

License:Apache License

@Test
public void clientSendAndServerReceiveTest() throws InterruptedException {
    final MessageRegistry messageRegistry = new MessageRegistry();
    final NioEventLoopGroup bossGroup = new NioEventLoopGroup();
    final NioEventLoopGroup workerGroup = new NioEventLoopGroup();
    final BlockingQueue<Object> serverMessages = new LinkedBlockingQueue<>();
    final NodeAddress address = new NodeAddress("a", 8001);

    NodeServer server = new NodeServer();
    server.initialize("127.0.0.1", 8042, bossGroup, workerGroup, messageRegistry,
            new ChannelInboundHandlerAdapter() {
                @Override// www.ja  v  a 2  s  .  c o  m
                public void channelRead(ChannelHandlerContext ctx, Object msg) {
                    serverMessages.offer(msg);
                }
            });

    NodeClient client = new NodeClient();
    client.initialize("127.0.0.1", 8042, workerGroup, messageRegistry, new ChannelInboundHandlerAdapter());
    client.send(new JoinRequest(new NodeId(42), address));
    client.send(new JoinResponse(JoinResponse.ResponseCode.OK, new NodeId(42), address));

    Object msg = serverMessages.poll(5, TimeUnit.SECONDS);
    Assert.assertNotNull(msg);
    Assert.assertEquals(msg.getClass(), JoinRequest.class);
    Assert.assertEquals(((JoinRequest) msg).getJoinerId(), new NodeId(42));
    Assert.assertEquals(((JoinRequest) msg).getJoinerAddress(), address);

    msg = serverMessages.poll(5, TimeUnit.SECONDS);
    Assert.assertNotNull(msg);
    Assert.assertEquals(msg.getClass(), JoinResponse.class);
    Assert.assertEquals(((JoinResponse) msg).getResult(), JoinResponse.ResponseCode.OK);

    client.shutdown();
    server.shutdown();
    bossGroup.shutdownGracefully();
    workerGroup.shutdownGracefully();
}

From source file:com.linkedin.r2.transport.http.client.TestHttpClientFactory.java

License:Apache License

/**
 * Tests that even when the factory is shutdown with a long timeout, it does not occupy
 * any executors with tasks that might prevent them shutting down properly.
 * @throws InterruptedException//w w w.  j  a  va 2s .  c o m
 * @throws ExecutionException
 * @throws TimeoutException
 */
@Test
public void testShutdownTimeoutDoesNotOccupyExecutors()
        throws InterruptedException, ExecutionException, TimeoutException {
    NioEventLoopGroup eventLoop = new NioEventLoopGroup();
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    HttpClientFactory factory = getHttpClientFactory(eventLoop, false, scheduler, false);

    FutureCallback<None> callback = new FutureCallback<None>();
    factory.shutdown(callback, 60, TimeUnit.MINUTES);
    callback.get(60, TimeUnit.SECONDS);
    scheduler.shutdown();
    eventLoop.shutdownGracefully();
    Assert.assertTrue(scheduler.awaitTermination(60, TimeUnit.SECONDS));
    Assert.assertTrue(eventLoop.awaitTermination(60, TimeUnit.SECONDS));
}