NettyClientFactory extends AbstractStartStoppable implements PooledObjectFactory<NettyClient> { @Override public PooledObject<NettyClient> makeObject() throws Exception { ChannelFuture f = b.connect(endpoint.getHost(), endpoint.getPort()); f.get(connectTimeoutMilli, TimeUnit.MILLISECONDS); Channel channel = f.channel(); sendProxyProtocolIfNeeded(channel); logger.info("[makeObject]{}", channel); NettyClient nettyClient = new DefaultNettyClient(channel); channel.attr(NettyClientHandler.KEY_CLIENT).set(nettyClient); return new DefaultPooledObject<NettyClient>(nettyClient); } NettyClientFactory(Endpoint endpoint); @Override PooledObject<NettyClient> makeObject(); @Override void destroyObject(PooledObject<NettyClient> p); @Override boolean validateObject(PooledObject<NettyClient> p); @Override void activateObject(PooledObject<NettyClient> p); @Override void passivateObject(PooledObject<NettyClient> p); @Override String toString();  }
@Test public void testMakeObject() throws Exception { PooledObject<NettyClient> pooledObject = factory.makeObject(); NettyClient client = pooledObject.getObject(); PooledByteBufAllocator allocator = (PooledByteBufAllocator)client.channel().alloc(); AtomicReference<Object> freeSweepAllocationThreshold = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); client.channel().eventLoop().execute(new Runnable() { @Override public void run() { try { Field field = FieldUtils.getDeclaredField(allocator.getClass(), "threadCache", true); Object threadCache = field.get(allocator); logger.info("[class]{}", threadCache.getClass()); FastThreadLocal fastThreadLocal = (FastThreadLocal) threadCache; Object poolThreadCache = fastThreadLocal.get(); logger.info("[class]{}", poolThreadCache.getClass()); Field param = FieldUtils.getField(poolThreadCache.getClass(), "freeSweepAllocationThreshold", true); logger.info("[{}]", param.get(poolThreadCache)); freeSweepAllocationThreshold.set(param.get(poolThreadCache)); } catch (IllegalAccessException e) { logger.error("", e); } latch.countDown(); } }); latch.await(); Assert.assertNotNull(freeSweepAllocationThreshold.get()); Assert.assertEquals(0, freeSweepAllocationThreshold.get()); }