Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Akka, can I spoof a message from a different actor?

Tags:

scala

akka

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

like image 308
Rob Wilton Avatar asked Nov 05 '12 21:11

Rob Wilton


2 Answers

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.

like image 170
jsuereth Avatar answered Sep 22 '22 15:09

jsuereth


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)

like image 45
oluies Avatar answered Sep 20 '22 15:09

oluies