spierce7
spierce7

Reputation: 15746

Accumulating and Ordering Child Actor Responses?

I'm new to Scala and Akka, and I've been reading a book on Akka. I can't find a reasonable solution for what I would think is a common use case with with Actors.

Lets say you have a parent actor that receives a request for a large chunk of work (Say you need to go to the network to download 100 files), and so the parent actor splits up the work and routes it to 10 children, so that we are downloading more than 1 file at once.

Somehow I need to get all of the files back to the parent actor in order. What would be a good design pattern for doing this?

I found a link in my searching where they seem to have come up with a good way of accomplishing this, but because the blog don't actually show how to use the example (as they just providing a code snippet), and I'm a scala noob, I don't understand how to put it in practice: http://www.ccri.com/2014/01/22/accumulating-responses-from-child-actors-and-transitive-message-ordering/

Upvotes: 2

Views: 150

Answers (1)

tariksbl
tariksbl

Reputation: 1089

It can be done with akka futures. From your parent actor:

  1. Create an array of 100 futures, each of which fetches one file
  2. Use Future.sequence or .traverse to map the array of futures to a future of an array of files
  3. map that to .pipe to send the array back to the parent actor

Each future could be the result of an ask to a pool of child actors, if fetching each file is best suited to an actor not a future.

Upvotes: 3

Related Questions