@Override protected EventLoopGroup group() { return new DefaultEventLoopGroup(); }
@Override public void shutdown() { if (defLoopGroup != null) { defLoopGroup.shutdownGracefully(); } bossGroup.shutdownGracefully(); workGroup.shutdownGracefully(); logger.info("Server EventLoopGroup shutdown finish"); }
public void init(){ defLoopGroup = new DefaultEventLoopGroup(8, new ThreadFactory() { private AtomicInteger index = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { return new Thread(r, "DEFAULTEVENTLOOPGROUP_" + index.incrementAndGet()); } }); bossGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(), new ThreadFactory() { private AtomicInteger index = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { return new Thread(r, "BOSS_" + index.incrementAndGet()); } }); workGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 10, new ThreadFactory() { private AtomicInteger index = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { return new Thread(r, "WORK_" + index.incrementAndGet()); } }); b = new ServerBootstrap(); }
@Override public void close() { List<HDBConnection> connectionsAtClose = new ArrayList<>(this.connections.values()); for (HDBConnection connection : connectionsAtClose) { connection.close(); } if (networkGroup != null) { networkGroup.shutdownGracefully(); } if (localEventsGroup != null) { localEventsGroup.shutdownGracefully(); } if (thredpool != null) { thredpool.shutdown(); } }
public void init(){ defLoopGroup = new DefaultEventLoopGroup(8, new ThreadFactory() { private AtomicInteger index = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { return new Thread(r, "DEFAULTEVENTLOOPGROUP_" + index.incrementAndGet()); } }); bossGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(), new ThreadFactory() { private AtomicInteger index = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { return new Thread(r, "BOSS_" + index.incrementAndGet()); } }); workGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 10, new ThreadFactory() { private AtomicInteger index = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { return new Thread(r, "WORK_" + index.incrementAndGet()); } }); b = new ServerBootstrap(); }
@Override public void shutdown() { if (defLoopGroup != null) { defLoopGroup.shutdownGracefully(); } bossGroup.shutdownGracefully(); workGroup.shutdownGracefully(); } }
/** Get an {@link io.netty.channel.EventLoopGroup} for Netty. * * @param numThreads The number of threads in the {@link EventLoopGroup}. * @param nameFormat A format string for the threads in the {@link EventLoopGroup} * @return The {@link EventLoopGroup} generated. */ private static EventLoopGroup getEventLoopGroup(int numThreads, @Nonnull String nameFormat) { return new DefaultEventLoopGroup(numThreads, new ThreadFactoryBuilder() .setNameFormat(nameFormat) .setDaemon(true) .build()); } }
@Override public void shutdown() { if (defLoopGroup != null) { defLoopGroup.shutdownGracefully(); } bossGroup.shutdownGracefully(); workGroup.shutdownGracefully(); }
@BeforeClass public static void initEventGroup() { NETTY_EVENT_LOOP = new DefaultEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2, new ThreadFactoryBuilder() .setNameFormat("netty-%d") .setDaemon(true) .setUncaughtExceptionHandler((thread, throwable) -> { assertThat(false) .as("Thread " + thread.getName() + " unexpectedly terminated with " + throwable.getClass().getSimpleName()) .isTrue(); }) .build()); }
localBossGroup = new DefaultEventLoopGroup(workerThreads); localWorkerGroup = new DefaultEventLoopGroup(workerThreads); ServerBootstrap b_local = new ServerBootstrap(); b_local.group(localBossGroup, localWorkerGroup)
@Before public void setUp() { EventLoopGroup serverGroup = new DefaultEventLoopGroup(); ServerBootstrap sb = new ServerBootstrap(); sb.group(serverGroup) .channel(LocalServerChannel.class) .handler( new ChannelInitializer<LocalServerChannel>() { @Override public void initChannel(LocalServerChannel ch) throws Exception {} }) .childHandler( new ChannelInitializer<LocalChannel>() { @Override public void initChannel(LocalChannel ch) throws Exception {} }); ChannelFuture future = sb.bind(address); future.awaitUninterruptibly(); server = (LocalServerChannel) future.channel(); }
@Test public void disposeLaterDefers() { PooledConnectionProvider.Pool pool = new PooledConnectionProvider.Pool( new Bootstrap().group(new DefaultEventLoopGroup()), (b, handler, checker) -> channelPool, ChannelOperations.OnSetup.empty()); PooledConnectionProvider poolResources = new PooledConnectionProvider("test", (b, handler, checker) -> channelPool); //"register" our fake Pool poolResources.channelPools.put( new PooledConnectionProvider.PoolKey( InetSocketAddress.createUnresolved("localhost", 80), -1), pool); Mono<Void> disposer = poolResources.disposeLater(); assertThat(closed.get()).as("pool closed by disposeLater()").isEqualTo(0); disposer.subscribe(); assertThat(closed.get()).as("pool closed by disposer subscribe()").isEqualTo(1); }
@Test public void testConnectionClosed() { try (ConnectionPool pool = new ConnectionPool(new TestingConnectionManager(), new DefaultEventLoopGroup(), 10, new Duration(1, MINUTES))) { HostAndPort address = HostAndPort.fromParts("localhost", 1234); Channel channel1 = futureGet(pool.getConnection(PARAMETERS, address)); assertTrue(channel1.isOpen()); channel1.close(); assertFalse(channel1.isOpen()); Channel channel2 = futureGet(pool.getConnection(PARAMETERS, address)); assertTrue(channel2.isOpen()); assertNotSame(channel1, channel2); } }
@Test public void testConnectionClosed() { try (ConnectionPool pool = new ConnectionPool(new TestingConnectionManager(), new DefaultEventLoopGroup(), 10, new Duration(1, MINUTES))) { HostAndPort address = HostAndPort.fromParts("localhost", 1234); Channel channel1 = futureGet(pool.getConnection(PARAMETERS, address)); assertTrue(channel1.isOpen()); channel1.close(); assertFalse(channel1.isOpen()); Channel channel2 = futureGet(pool.getConnection(PARAMETERS, address)); assertTrue(channel2.isOpen()); assertNotSame(channel1, channel2); } }
acceptorType = INVM_ACCEPTOR_TYPE; channelClazz = LocalServerChannel.class; eventLoopGroup = new DefaultEventLoopGroup(); } else {
}); this.networkGroup = NetworkUtils.isEnableEpoolNative() ? new EpollEventLoopGroup() : new NioEventLoopGroup(); this.localEventsGroup = new DefaultEventLoopGroup(); String mode = configuration.getString(ClientConfiguration.PROPERTY_MODE, ClientConfiguration.PROPERTY_MODE_LOCAL); switch (mode) {
@Test public void disposeOnlyOnce() { PooledConnectionProvider.Pool pool = new PooledConnectionProvider.Pool( new Bootstrap().group(new DefaultEventLoopGroup()), (b, handler, checker) -> channelPool, ChannelOperations.OnSetup.empty() ); PooledConnectionProvider poolResources = new PooledConnectionProvider("test", (b, handler, checker) -> channelPool); //"register" our fake Pool poolResources.channelPools.put( new PooledConnectionProvider.PoolKey( InetSocketAddress.createUnresolved("localhost", 80), -1), pool); poolResources.dispose(); assertThat(closed.get()).as("pool closed by dispose()").isEqualTo(1); Mono<Void> disposer = poolResources.disposeLater(); disposer.subscribe(); poolResources.disposeLater().subscribe(); poolResources.dispose(); assertThat(closed.get()).as("pool closed only once").isEqualTo(1); }
@Test public void testConnect() throws ExecutionException { EventLoopGroup group = new DefaultEventLoopGroup(); Connector connector = new Connector(address) {
@Test public void testPooling() { try (ConnectionPool pool = new ConnectionPool(new TestingConnectionManager(), new DefaultEventLoopGroup(), 10, new Duration(1, MINUTES))) { HostAndPort address1 = HostAndPort.fromParts("localhost", 1234); HostAndPort address2 = HostAndPort.fromParts("localhost", 4567); Channel channel1 = futureGet(pool.getConnection(PARAMETERS, address1)); Channel channel2 = futureGet(pool.getConnection(PARAMETERS, address1)); assertSame(channel1, channel2); Channel channel3 = futureGet(pool.getConnection(PARAMETERS, address2)); assertNotSame(channel1, channel3); Channel channel4 = futureGet(pool.getConnection(PARAMETERS, address1)); assertSame(channel1, channel4); } }
@Test public void testPooling() { try (ConnectionPool pool = new ConnectionPool(new TestingConnectionManager(), new DefaultEventLoopGroup(), 10, new Duration(1, MINUTES))) { HostAndPort address1 = HostAndPort.fromParts("localhost", 1234); HostAndPort address2 = HostAndPort.fromParts("localhost", 4567); Channel channel1 = futureGet(pool.getConnection(PARAMETERS, address1)); Channel channel2 = futureGet(pool.getConnection(PARAMETERS, address1)); assertSame(channel1, channel2); Channel channel3 = futureGet(pool.getConnection(PARAMETERS, address2)); assertNotSame(channel1, channel3); Channel channel4 = futureGet(pool.getConnection(PARAMETERS, address1)); assertSame(channel1, channel4); } }