Home > Back-end >  ActorReferences as member variables in other actors in Akka
ActorReferences as member variables in other actors in Akka

Time:10-31

Akka has shown two preferred ways of getting actor references of different actors in another actors, one is using Receptionist.Find() and the other one is by subscribing to the receptionist for the specific actor key.

Say I have an actor hierarchy where Actor A spawns Actor B as a child. If I need to send a message to Actor B, how would I do it? Should I have B register with the receptionist and then send a message, or can I create a member variable of ActorReference<B> in Actor A, which I can use to send messages to Actor B?

CodePudding user response:

When actor A spawns actor B, it will get an ActorRef for B. It can then freely save that reference in its state. There are also no restrictions on A handing the reference to B to another actor in a message (and the recipient actor may then freely save that reference in its state, etc.).

It's generally reasonable for actors to know the identity of the actors they collaborate with, just as it's generally reasonable for people to know the identity of their coworkers or children. The receptionist is basically for setting up the "initial rendezvous".

  • Related