I'm using Akka to control access to running system processes.
I have a single CommandActor that handles incoming command requests from any actor in the system (lets call it the RequestActor), and for each new request the CommandActor spawns a separate CmdChildWorker actor to handle that particular request. The CommandActor also limits the number of CmdChildWorkers, and assigns a unique id to each request, so it is more complicated than a simple router.
When the command completes, and periodically during the execution of the system process, the CmdChildWorker actors sends periodic updates back to the original RequestActor (e.g. process output so far).
However, to maintain a clean design, I was hoping that I would be able to keep the CmdChildWorker completely hidden from the original RequestActor, with its only interface being to the single CommandActor.
Obviously, I could send any reply messages from the CmdChildWorker back via the CommandActor, but I was wondering if it is possible to reply back to the RequestActor directly from the CmdChildWorker without having to route the message via the CommandActor, but still pretending that the message has been sent back from the CommandActor.
I.e. I would like to spoof the CmdChildWorker actors senders address to be that of its parent actor. Is this possible? And perhaps more importantly is this sensible, or good actor design?
Thanks
See the tell
method on actor. When the CommandActor forwards its message, call:
childActor.tell(msg, sender)
and the childActor will have its sender as the original sender.
If I understand your requironment correctly you should use forward. From the docs:
You can forward a message from one actor to another. This means that the original sender address/reference is maintained even though the message is going through a 'mediator'. This can be useful when writing actors that work as routers, load-balancers, replicators etc.
myActor.forward(message)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With