r4bb1t
r4bb1t

Reputation: 1165

ActorReferences as member variables in other actors in Akka

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?

Upvotes: 0

Views: 177

Answers (1)

Levi Ramsey
Levi Ramsey

Reputation: 20551

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".

Upvotes: 2

Related Questions