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.relayrides.pushy.apns.BenchmarkApp.java

License:Open Source License

public void runAllBenchmarks() throws InterruptedException {
    this.serverEventLoopGroup = new NioEventLoopGroup(2);
    this.server = new MockApnsServer(GATEWAY_PORT, serverEventLoopGroup);

    // We want to do a dummy run first to let the JVM warm up
    final NioEventLoopGroup warmupGroup = new NioEventLoopGroup(1);
    this.runBenchmark(warmupGroup, 1, this.notifications);
    warmupGroup.shutdownGracefully().await();

    System.out.println("threads, connections, throughput [k/sec]");

    for (int eventLoopGroupThreadCount : new int[] { 1, 2, 4, 8, 16 }) {
        final NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(eventLoopGroupThreadCount);

        for (int concurrentConnectionCount : new int[] { 1, 2, 4, 8, 16, 32 }) {
            if (concurrentConnectionCount >= eventLoopGroupThreadCount) {
                System.gc();/*from www .jav a  2 s  . c  om*/

                double throughput = this.runBenchmark(eventLoopGroup, concurrentConnectionCount,
                        this.notifications);

                System.out.format("%d, %d, %.1f\n", eventLoopGroupThreadCount, concurrentConnectionCount,
                        throughput / 1000.0);
            }
        }

        eventLoopGroup.shutdownGracefully().await();
    }

    this.serverEventLoopGroup.shutdownGracefully().await();
}

From source file:com.relayrides.pushy.apns.PushManagerTest.java

License:Open Source License

@Test
public void testShutdown() throws Exception {
    {/*from w  ww.  ja  v a 2 s.  c  om*/
        final PushManager<ApnsPushNotification> defaultGroupPushManager = new PushManager<ApnsPushNotification>(
                TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient(), 1, null, null, null,
                ApnsConnection.DEFAULT_SENT_NOTIFICATION_BUFFER_CAPACITY);

        defaultGroupPushManager.start();
        defaultGroupPushManager.shutdown();

        assertTrue(defaultGroupPushManager.isShutDown());
    }

    {
        final NioEventLoopGroup group = new NioEventLoopGroup(1);

        final PushManager<ApnsPushNotification> providedGroupPushManager = new PushManager<ApnsPushNotification>(
                TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient(), 1, group, null, null,
                ApnsConnection.DEFAULT_SENT_NOTIFICATION_BUFFER_CAPACITY);

        providedGroupPushManager.start();
        providedGroupPushManager.shutdown();

        assertTrue(providedGroupPushManager.isShutDown());
        assertFalse(group.isShutdown());

        group.shutdownGracefully();
    }

    {
        final ExecutorService listenerExecutorService = Executors.newSingleThreadExecutor();

        final PushManager<ApnsPushNotification> providedExecutorServicePushManager = new PushManager<ApnsPushNotification>(
                TEST_ENVIRONMENT, SSLTestUtil.createSSLContextForTestClient(), 1, null, listenerExecutorService,
                null, ApnsConnection.DEFAULT_SENT_NOTIFICATION_BUFFER_CAPACITY);

        providedExecutorServicePushManager.start();
        providedExecutorServicePushManager.shutdown();

        assertTrue(providedExecutorServicePushManager.isShutDown());
        assertFalse(listenerExecutorService.isShutdown());

        listenerExecutorService.shutdown();
    }
}

From source file:com.turo.pushy.apns.MockApnsServerTest.java

License:Open Source License

@Test
public void testShutdownWithProvidedEventLoopGroup() throws Exception {
    final NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(1);

    try {/*ww  w .  java  2s .c om*/

        final MockApnsServer providedGroupServer = new MockApnsServerBuilder()
                .setServerCredentials(
                        MockApnsServerTest.class.getResourceAsStream(SERVER_CERTIFICATES_FILENAME),
                        MockApnsServerTest.class.getResourceAsStream(SERVER_KEY_FILENAME), null)
                .setEventLoopGroup(eventLoopGroup).build();

        assertTrue(providedGroupServer.start(PORT).await().isSuccess());
        assertTrue(providedGroupServer.shutdown().await().isSuccess());

        assertFalse(eventLoopGroup.isShutdown());
    } finally {
        eventLoopGroup.shutdownGracefully();
    }
}

From source file:com.turo.pushy.apns.MockApnsServerTest.java

License:Open Source License

@Test
public void testRestartWithProvidedEventLoopGroup() throws Exception {
    final NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(1);

    try {//from  ww  w . j  a  v a  2s. c  om

        final MockApnsServer providedGroupServer = new MockApnsServerBuilder()
                .setServerCredentials(
                        MockApnsServerTest.class.getResourceAsStream(SERVER_CERTIFICATES_FILENAME),
                        MockApnsServerTest.class.getResourceAsStream(SERVER_KEY_FILENAME), null)
                .setEventLoopGroup(eventLoopGroup).build();

        assertTrue(providedGroupServer.start(PORT).await().isSuccess());
        assertTrue(providedGroupServer.shutdown().await().isSuccess());
        assertTrue(providedGroupServer.start(PORT).await().isSuccess());
        assertTrue(providedGroupServer.shutdown().await().isSuccess());
    } finally {
        eventLoopGroup.shutdownGracefully();
    }
}

From source file:com.turo.pushy.apns.server.MockApnsServerTest.java

License:Open Source License

@Test
public void testShutdownWithProvidedEventLoopGroup() throws Exception {
    final NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(1);

    try {// www.java2s  .c  o m

        final MockApnsServer providedGroupServer = new MockApnsServerBuilder()
                .setServerCredentials(getClass().getResourceAsStream(SERVER_CERTIFICATES_FILENAME),
                        getClass().getResourceAsStream(SERVER_KEY_FILENAME), null)
                .setHandlerFactory(new AcceptAllPushNotificationHandlerFactory())
                .setEventLoopGroup(eventLoopGroup).build();

        assertTrue(providedGroupServer.start(PORT).await().isSuccess());
        assertTrue(providedGroupServer.shutdown().await().isSuccess());

        assertFalse(eventLoopGroup.isShutdown());
    } finally {
        eventLoopGroup.shutdownGracefully().await();
    }
}

From source file:com.turo.pushy.apns.server.MockApnsServerTest.java

License:Open Source License

@Test
public void testRestartWithProvidedEventLoopGroup() throws Exception {
    int javaVersion = 0;

    try {//from w w  w .j ava 2s  .c  o m
        javaVersion = Integer.parseInt(System.getProperty("java.specification.version"));
    } catch (final NumberFormatException ignored) {
    }

    // TODO Remove this assumption when https://github.com/netty/netty/issues/8697 gets resolved
    assumeTrue(javaVersion < 11);

    final NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(1);

    try {
        final MockApnsServer providedGroupServer = new MockApnsServerBuilder()
                .setServerCredentials(getClass().getResourceAsStream(SERVER_CERTIFICATES_FILENAME),
                        getClass().getResourceAsStream(SERVER_KEY_FILENAME), null)
                .setHandlerFactory(new AcceptAllPushNotificationHandlerFactory())
                .setEventLoopGroup(eventLoopGroup).build();

        assertTrue(providedGroupServer.start(PORT).await().isSuccess());
        assertTrue(providedGroupServer.shutdown().await().isSuccess());

        assertTrue(providedGroupServer.start(PORT).await().isSuccess());
        assertTrue(providedGroupServer.shutdown().await().isSuccess());
    } finally {
        eventLoopGroup.shutdownGracefully().await();
    }
}

From source file:com.xiovr.unibot.bot.network.BotConnectionTest.java

License:Open Source License

@SuppressWarnings("null")
@Test()//from   w  ww.  j  av a2s .  c  o  m
public void testBotConnection_check_connect_to_echo_server() throws InterruptedException {

    NioEventLoopGroup workerGroup = new NioEventLoopGroup();
    try {
        logger.info("Start echo server for BotConnection test");
        System.out.println("Start echo server for BotConnection test");
        InetSocketAddress address = new InetSocketAddress("localhost", 8888);
        EchoServer echoServer = new EchoServer(address);
        echoServer.startServer();

        try {
            while (!echoServer.getStarted()) {
                Thread.sleep(100);
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        logger.info("Echo server is started");
        System.out.println("Echo server is started");

        RingBufferPool<Packet> buffer = new RingBufferPacketPoolImpl();
        BotContext botContext = new BotContextImpl();
        botContext.setReadBuffer(buffer);
        BotConnection botConnection = new BotConnectionServerImpl();
        botConnection.init(workerGroup, botContext, 0);
        botContext.addServerConnection(botConnection);

        // Check two times
        for (int i = 0; i < 2; ++i) {
            botConnection.connect(address);
            Thread.sleep(100);
            Packet pck = PacketPool.obtain();

            // pck.putUtf16String("Hello!!");
            pck.writeS("Hello!!");
            pck.writeD(1000);
            pck.writeC(0x10);
            pck.writeD(0x11000);
            pck.writeF(092032.09);
            pck.writeQ(023423L);
            pck.writeB(new byte[] { 10, 10, 10 });
            pck.putHeader();
            botConnection.write(pck);
            botConnection.flush();
            Thread.sleep(100);
            Assert.assertEquals(buffer.count(), 1);
            // Packet pck2 = buffer.poll(PacketPool.obtain());
            Packet pck2 = buffer.poll();

            pck = PacketPool.obtain();
            pck.clear();
            pck.writeS("Hello!!");
            pck.writeD(1000);
            pck.writeC(0x10);
            pck.writeD(0x11000);
            pck.writeF(092032.09);
            pck.writeQ(023423L);
            pck.writeB(new byte[] { 10, 10, 10 });
            pck.putHeader();

            pck.clear();
            pck2.clear();

            Assert.assertEquals(pck2.getHeader(), pck.getHeader());
            Assert.assertEquals(pck2.readS(), pck.readS());
            Assert.assertEquals(pck2.readD(), pck.readD());
            Assert.assertEquals(pck2.readC(), pck.readC());
            Assert.assertEquals(pck2.readD(), pck.readD());
            Assert.assertEquals(pck2.readF(), pck.readF());
            Assert.assertEquals(pck2.readQ(), pck.readQ());

            botConnection.disconnect();
            Thread.sleep(100);
        }
        echoServer.close();
        // } catch (Exception e) {
        // e.printStackTrace();
    } finally {
        workerGroup.shutdownGracefully();
    }
}

From source file:com.yahoo.pulsar.discovery.service.DiscoveryServiceTest.java

License:Apache License

/**
 * It verifies: client connects to Discovery-service and receives discovery response successfully.
 * //  w ww .j a  v  a  2s. com
 * @throws Exception
 */
@Test
public void testClientServerConnection() throws Exception {
    addBrokerToZk(2);
    // 1. client connects to DiscoveryService, 2. Client receive service-lookup response
    final int messageTransfer = 2;
    final CountDownLatch latch = new CountDownLatch(messageTransfer);
    NioEventLoopGroup workerGroup = connectToService(service.getServiceUrl(), latch, false);
    try {
        assertTrue(latch.await(1, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
        fail("should have received lookup response message from server", e);
    }
    workerGroup.shutdownGracefully();
}

From source file:com.yahoo.pulsar.discovery.service.DiscoveryServiceTest.java

License:Apache License

@Test(enabled = true)
public void testClientServerConnectionTls() throws Exception {
    addBrokerToZk(2);//from ww  w.  j a v a2s  .c o  m
    // 1. client connects to DiscoveryService, 2. Client receive service-lookup response
    final int messageTransfer = 2;
    final CountDownLatch latch = new CountDownLatch(messageTransfer);
    NioEventLoopGroup workerGroup = connectToService(service.getServiceUrlTls(), latch, true);
    try {
        assertTrue(latch.await(1, TimeUnit.SECONDS));
    } catch (InterruptedException e) {
        fail("should have received lookup response message from server", e);
    }
    workerGroup.shutdownGracefully();
}

From source file:io.aos.netty5.udt.echo.bytes.ByteEchoClient.java

License:Apache License

public static void main(String[] args) throws Exception {
    // Configure the client.
    final ExecutorServiceFactory connectFactory = new DefaultExecutorServiceFactory("connect");
    final NioEventLoopGroup connectGroup = new NioEventLoopGroup(1, connectFactory,
            NioUdtProvider.BYTE_PROVIDER);

    try {/*from   w  w w. j a  v  a 2 s  . co  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();
    }
}