Netty channelAcquired没有被调用

时间:2015-10-15 05:31:20

标签: java events netty

我正在为http客户端使用netty通道池,而ChannelPoolHandler实现channelAcquired在调用channelPool.acquire()时没有被调用。我正在使用netty 4.0.32.Final。这是我创建chanelpool的方式。我只是按照netty.io上列出的简单示例进行操作。如果有人能够解释我做错了什么,或者是否有一个非常有帮助的错误。感谢。

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());
        }
    }
});

1 个答案:

答案 0 :(得分:1)

channelAcquired方法仅在您获得&#34;先前创建的频道。在您的情况下,池中还没有频道,因此它会调用channelCreated