2015-10-15 98 views
0

我为一个http客户端使用netty通道池,在ChannelPoolHandler实现中channelAcquired在调用channelPool.acquire()时没有被调用。我正在使用netty 4.0.32.Final。以下是我创建chanelpool的方式。我只是遵循netty.io中列出的简单示例。如果有人能够解释我做错了什么,或者是否有一个会非常有用的错误。谢谢。Netty channelAcquired没有被调用

EventLoopGroup group = new NioEventLoopGroup(); 
final Bootstrap b = new Bootstrap(); 
b.group(group).channel(NioSocketChannel.class); 
AbstractChannelPoolMap<InetSocketAddress, SimpleChannelPool> poolMap = new AbstractChannelPoolMap<InetSocketAddress, SimpleChannelPool>() { 
    @Override 
    protected SimpleChannelPool newPool(InetSocketAddress key) { 
     return new SimpleChannelPool(b.remoteAddress(key), new HttpClientPoolHandler()); 
    } 
}; 

final SimpleChannelPool simpleChannelPool = poolMap.get(new InetSocketAddress(uri.getHost(), uri.getPort())); 
final Future<Channel> acquire = simpleChannelPool.acquire(); 

acquire.addListener(new FutureListener<Channel>() { 
    public void operationComplete(Future<Channel> f) throws Exception { 
     if (f.isSuccess()) { 
      final Channel ch = f.getNow(); 
      // Send the HTTP request. 
      ChannelFuture channelFuture = ch.writeAndFlush(request); 
      channelFuture.addListener(new ChannelFutureListener() { 
       public void operationComplete(ChannelFuture channelFuture) throws Exception { 
        if (channelFuture.isSuccess()) { 
         simpleChannelPool.release(ch); 
        } else { 
        } 
       } 
      }); 
     } else { 
      System.out.println("ERROR : " + f.cause()); 
     } 
    } 
}); 
+0

看看netty源代码,我发现'channelAcquired'方法只在池中有一个先前释放的通道时被调用。否则创建一个新通道而不调用'channelAcquired'方法。这是它应该的方式吗? – Sudheera

回答

1

channelAcquired方法将只在您“获取”一个先前创建的通道时被调用。在你的情况下,池中还没有频道,因此它会调用channelCreated

相关问题