Netty ServerBootstrap - 异步绑定?

时间:2012-06-23 04:06:04

标签: asynchronous jboss bind netty

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

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

祝你好运

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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