【发布时间】:2017-07-11 04:23:27
【问题描述】:
我有一个包含 ip/port 信息的 hashmap 和一条必须发送到整个列表的消息。 所以我决定创建一个小方法来接受 hashmap 和消息并执行此操作。它看起来像这样:
public static ChannelFuture sendMessageTo(Map<JsonElement, JsonObject> list, String message) {
Set<JsonElement> keys = list.keySet();
for (JsonElement key : keys) { //iterate through the map
ChannelInboundHandler[] handlers = {
new MessageCompletenessHandler(),
new MessageRequest(message),
};
JsonObject identity = list.get(key);
ChannelFuture f = connectWithHandler(identity.get("ip").getAsString(), identity.get("port").getAsInt(), handlers); //to the following ip/port send message and return ChannelFuture
}
return result; //here result should be a ChannelFuture that when .addListener is added it should be called only when ALL the ChannelFuture-s from the for loop have finished(a.k.a. all messages have been sent)
}
cmets 应该足够清楚地解释情况。 问题是如何实现这个 ChannelFuture 结果。 我知道我可以 .sync() ChannelFuture-s,但这违背了异步网络的目的。
P.S.:我本质上想拥有这里描述的功能https://twistedmatrix.com/documents/16.2.0/api/twisted.internet.defer.DeferredList.html,但我找不到等效功能。
【问题讨论】:
标签: java asynchronous netty