0
votes

The examples of creating a custom Akka router (such as http://doc.akka.io/docs/akka/snapshot/scala/routing.html#custom-router-scala) show the router returning a list of target actor/sender pairs in order to tell Akka what underlying actors to send a message to. Why is this pattern preferred to the actor simply using tell() to send messages to those actors?

Specifically, would there be anything wrong with using tell() in a router instead, potentially asynchronously, i.e. later on from another thread than the thread calling the routing function?

1

1 Answers

2
votes

1) Why is this pattern preferred to the actor simply using tell() to send messages to those actors?

Because the router has more functionality than just sending to routees (like resizing for example). It's a good separation of logic to differentiate the logic of determining the routees from the rest of the logic involved in routing a message. This allows those different pieces to change independent from one another. Also, if you just start doing additional tells inside of createRoute, that's not very functional as it's side effecting which you should try and avoid.

2) Specifically, would there be anything wrong with using tell() in a router instead, potentially asynchronously, i.e. later on from another thread than the thread calling the routing function?

While this is possible, as I mentioned above, it's probably not a good idea. This kind of side effecting can be extremely difficult to test and it's not a good fit within the functional paradigm. If you really need something like this, then maybe just use an Actor instead of a Router for your routing logic. The routers are meant to give you a simple model to fit into for performing routing. If your requirements don't fit this model then it's possible that a router is not the right fit.