Sheshadri Mantha
Sheshadri Mantha

Reputation: 491

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...

Upvotes: 1

Views: 4063

Answers (2)

Sheshadri Mantha
Sheshadri Mantha

Reputation: 491

OK - solved it by doing the following:

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

where

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

and futures is the collection of futures i.e., ArrayList<Future<Boolean>>.

Upvotes: 4

Gian
Gian

Reputation: 13955

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.

Upvotes: 1

Related Questions