5

首先,这是我在哪里阅读我现在所知道的关于这个问题的所有内容的参考:http: //docs.jboss.org/netty/3.2/api/org/jboss/netty/bootstrap/ServerBootstrap.html#bind%28 %29

尽管文档没有明确指定,但它似乎ServerBootstrap.bind是同步的 - 因为它不返回 a ChannelFuture,而是返回 Channel。如果是这种情况,那么我看不到使用ServerBootstrap该类进行异步绑定的任何方法。我错过了什么还是我必须推出自己的解决方案?

此致

4

2 回答 2

4

我最终推出了自己的引导程序实现,并添加了以下内容:

public ChannelFuture bindAsync(final SocketAddress localAddress)
{
    if (localAddress == null) {
        throw new NullPointerException("localAddress");
    }
    final BlockingQueue<ChannelFuture> futureQueue =
        new LinkedBlockingQueue<ChannelFuture>();
    ChannelHandler binder = new Binder(localAddress, futureQueue);
    ChannelHandler parentHandler = getParentHandler();
    ChannelPipeline bossPipeline = pipeline();
    bossPipeline.addLast("binder", binder);
    if (parentHandler != null) {
        bossPipeline.addLast("userHandler", parentHandler);
    }
    getFactory().newChannel(bossPipeline);
    ChannelFuture future = null;
    boolean interrupted = false;
    do {
        try {
            future = futureQueue.poll(Integer.MAX_VALUE, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            interrupted = true;
        }
    } while (future == null);
    if (interrupted) {
        Thread.currentThread().interrupt();
    }
    return future;
}
于 2012-06-23T13:10:35.463 回答
0

在 Netty 3.6 中有一个异步绑定。这是javadoc: http: //netty.io/3.6/api/org/jboss/netty/bootstrap/ServerBootstrap.html#bindAsync()

于 2013-08-11T20:09:47.813 回答