2015-10-15 100 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

相關問題