首先,这是我阅读有关该问题的所有知识的引用:http://docs.jboss.org/netty/3.2/api/org/jboss/netty/bootstrap/ServerBootstrap.html#bind%28%29
尽管文档未明确指定,但ServerBootstrap.bind
似乎是同步的-因为它不返回ChannelFuture
,而是返回Channel。如果真是这样,那么我看不到使用ServerBootstrap
类进行异步绑定(bind)的任何方法。我是否缺少某些东西,或者我必须推出自己的解决方案?
此致
最佳答案
我最终使用以下附加功能滚动了自己的 bootstrap 实现:
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;
}