简体   繁体   English

Akka期货清单-如何等待期货清单完成?

[英]Akka List of futures - how to wait for list of futures to finish?

newbie here... 新手在这里...

I've got a very simple callable on a collection of domain objects. 我在域对象集合上有一个非常简单的可调用对象。 In using futures, all I care about is truly knowing when all the futures have completed. 使用期货时,我所关心的就是真正知道所有期货何时完成。

So I'm creating a collection of futures, iterating over my domain objects, creating a future (with the callable) and adding it to the collection of futures. 因此,我要创建一个期货集合,遍历我的域对象,创建一个期货(带有可调用对象)并将其添加到期货集合中。

Assuming that the callable returns a Boolean, how can I ascertain that all the futures have been completed. 假设可调用函数返回布尔值,那么我如何确定所有期货都已完成。

Oh! 哦! I want to wait till this collection is processed before I process another collection as the 2nd collection process is dependent on the 1st. 我想等到这个集合被处理后再处理另一个集合,因为第二个集合过程取决于第一个集合。

Any help appreciated. 任何帮助表示赞赏。

SAID DIFFERENTLY : How can I wait for all of items of an array to finish? 表示不同 :如何等待阵列中的所有项目完成? Do I have to iterate and wait on each? 我是否需要迭代并等待它们?

ArrayList<Future<Object>> responses = new ArrayList<Future<Object>>();

PS: coding in JAVA and also confused on use of PartialFunctions etc., on perhaps applying filter, counting and comparing against collection size as a possible solution... PS:用JAVA编码,并且对PartialFunctions等的使用也感到困惑 ,可能是应用过滤器,对集合大小进行计数和比较作为可能的解决方案...

OK - solved it by doing the following: OK-通过执行以下操作解决了该问题:

Await.result(seq, Duration.create("5 seconds"));

where 哪里

Future<Iterable<Boolean>> seq = Futures.sequence(futures, ec);

and futures is the collection of futures ie, ArrayList<Future<Boolean>> . futuresfutures的集合,即ArrayList<Future<Boolean>>

Taking the example from this article : 本文中的示例为例:

Future<Iterable<Long>> futuresSequence = sequence(futures);

// block until the futures come back
Iterable<Long> results = futuresSequence.get();

It appears that you're attempting to compose futures, but you've nested your collection and futures the wrong way around for this to work correctly. 您似乎正在尝试编写期货,但是您为正确执行嵌套错误的方式嵌套了收藏和期货。 Try making a Future of type Iterable instead, and then calling get on this future. 尝试改用IterableFuture ,然后在此future上调用get

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM