gpt4 book ai didi

asynchronous - Netty ServerBootstrap - 异步绑定(bind)?

转载 作者:行者123 更新时间:2023-12-04 20:16:48 27 4
gpt4 key购买 nike

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

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

最好的祝福

最佳答案

我最终推出了自己的 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;
}

关于asynchronous - Netty ServerBootstrap - 异步绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11166759/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com