0
votes

I'm new to AKKA (java) and I'm trying to implement the following using AKKA:

I receive a bunch of messages from an external process, and each message contains X numbers. I want to perform an operation on each number and then aggregate (sum) the results per message.

For example:

  • message1 - 1,3,5 --> doSomething(1)+doSomething(3)+doSomething(5)
  • message2 - 5,6 --> doSomething(5)+doSomething(6)

The "doSomething" code should be concurrent as well as the rest of my code, so I want to implement something like the following:

  • class Actor1 --> grabs a message from a queue and sends the messages to Actor2
  • class Actor2 --> splits the message to it's numbers and sends each "number" message to Actor3.
  • class Actor3 --> activates doSomething(number) and returns a result to Actor2.
  • class Actor2 --> will aggregate it's related message results (how will it know from where the "number" message cam from?? create a new actor system for that?) and return the aggregated result to Actor1. ...

How can I implement the aggregation using akka for java? I need to store a context. Seems that I have to create a pool of actor systems to make sure that each subgroup (message with numbers) is handled separately.

Any ideas? Is AKKA suitable for aggregated operations (that should be concurrent)?

1

1 Answers

1
votes

The scenario you describe is fairly straightforward to implement using the Ask pattern.

Actor 1 sends a message to Actor 2 to tell it the list of numbers. On receipt of the message Actor 2 stores the sender ActorRef in a property. This will be used later once the results have been aggregated so they can be sent back to Actor 1.

Actor 2 implements the logic of the ask pattern which asks Actor 3 for each number in the list. Actor 3 computes the result of doSomething() and returns it to sender (note - the sender wont be Actor 2 but rather an anonymous actor created to handle the future result of the ask).

Once all of the future asks are complete the results are aggregated and the Mapper applied to them.

Finally Actor 2 will pipe the aggregated and mapped results to the Actor 1 ActorRef stored at the start.