处理两种不同类型的未来(其中一种未来依赖于另一种未来)的理想方式是什么?

发布于 2024-11-23 22:40:59 字数 403 浏览 2 评论 0原文

我正在查看 AKKA 的 Java Futures API,我看到了很多处理同一类型的多个 future 的方法,但我没有看到任何处理不同类型的 future 的方法。我猜我让事情变得更加复杂了。

无论如何,假设我们有两个不同的 Actor:actorA 和 actorB。它们基于不同的类,并且各自返回不同的 Future。然而,actorB 依赖于 actorA 的 Future。是以下逻辑,如何最好地处理这种情况?

Future<A> a = actorA.sendRequestReplyFuture(...);

Future<B> b = actorB.sendRequestReplyFuture(a);

如果我们有一个 actorA 和 actorB 的列表怎么样?

I'm looking at AKKA's Java Futures API, and I see a lot of ways to handle multiple futures of the same type, but I don't see anything that pops out at me for handling futures of different types. I'm guessing that I'm making this more complicated than it is.

Anyways let's say we have two different Actors: actorA and actorB. They are based on different classes and they each return a different Future. However, actorB is dependent on a Future from actorA. Is the following logic, how to best handle this case?

Future<A> a = actorA.sendRequestReplyFuture(...);

Future<B> b = actorB.sendRequestReplyFuture(a);

How about if we have a list of actorAs and actorBs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微暖i 2024-11-30 22:40:59

警告,仅从内存中编写,可能无法编译 OOTB:

Future<B> b = actor.sendRequestReplyFuture().flatMap( new akka.japi.Function<A,Future<B>>() {
  public Future<B> apply(A a) {
     return actor.sendRequestReplyFuture(a);
  }
}

WARNING, just written from memory, might not compile OOTB:

Future<B> b = actor.sendRequestReplyFuture().flatMap( new akka.japi.Function<A,Future<B>>() {
  public Future<B> apply(A a) {
     return actor.sendRequestReplyFuture(a);
  }
}
南街九尾狐 2024-11-30 22:40:59

为什么不像您那样直接将它们包装起来,但只需稍作更改即可将 a 的结果传递给 b。

Future a = actorA.sendRequestReplyFuture(...);

Future b = actorB.sendRequestReplyFuture(a.get());

这种安排仅意味着在 actorA.sendRequestReplyFuture() 完成之前不会调用 actorB.sendRequestReplyFuture()。这没有什么问题,事实上,我喜欢你那里的模式。

Why not just wrap them, as you have, but with one slight change - pass the result of a into b.

Future a = actorA.sendRequestReplyFuture(...);

Future b = actorB.sendRequestReplyFuture(a.get());

This arrangement just means that actorB.sendRequestReplyFuture() won't get called until actorA.sendRequestReplyFuture() has finished. Nothing wrong with that, in fact, I like the pattern you've got there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文