创建自定义 Akka 路由器的示例(例如http://doc.akka.io/docs/akka/snapshot/scala/routing.html#custom-router-scala)显示路由器返回目标参与者/发送者列表为了告诉 Akka 将消息发送到哪些底层参与者。为什么简单地使用tell() 向那些actor 发送消息的actor 更喜欢这种模式?
具体来说,在路由器中使用 tell() 会不会有什么问题,可能是异步的,即稍后从另一个线程而不是调用路由函数的线程?
创建自定义 Akka 路由器的示例(例如http://doc.akka.io/docs/akka/snapshot/scala/routing.html#custom-router-scala)显示路由器返回目标参与者/发送者列表为了告诉 Akka 将消息发送到哪些底层参与者。为什么简单地使用tell() 向那些actor 发送消息的actor 更喜欢这种模式?
具体来说,在路由器中使用 tell() 会不会有什么问题,可能是异步的,即稍后从另一个线程而不是调用路由函数的线程?
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 tell
s 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.