当netty连接到我的服务器?

时间:2015-06-16 07:40:43

标签: java netty

我正在使用此处编写的Channle池API代码:

ChannelPool部分http://netty.io/news/2015/05/07/4-0-28-Final.html

EventLoopGroup group = new NioEventLoopGroup();
final Bootstrap cb = new Bootstrap();
InetSocketAddress addr1 = new InetSocketAddress("10.0.0.10", 8888);
InetSocketAddress addr2 = new InetSocketAddress("10.0.0.11", 8888);

cb.group(group).channel(NioSocketChannel.class);

ChannelPoolMap<InetSocketAddress, SimpleChannelPool> poolMap = new AbstractChannelPoolMap<InetSocketAddress, SimpleChannelPool>() {
    @Override
    protected SimpleChannelPool newPool(InetSocketAddress key) {
        return new SimpleChannelPool(cb.remoteAddress(key), new TestChannelPoolHandler());
    }
};

// depending on when you use addr1 or addr2 you will get different pools.
final SimpleChannelPool pool = poolMap.get(addr1);
Future<Channel> f = pool.acquire();
f.addListener(new FutureListener<Channel>() {
    @Override
    public void operationComplete(Future<Channel> f) {
        if (f.isSuccess()) {
            Channel ch = f.getNow();
            // Do somethings
            // ...
            // ...

            // Release back to pool
            pool.release(ch);
        }
    }
});

当我看到代码时,我们从未调用.connect方法,所以我的问题是netty试图将我的频道连接到服务器?

1 个答案:

答案 0 :(得分:1)

当您使用SimpleChannelPool时,它会在您调用acquire时进行连接,并且ChannelPool中没有任何内容。