Reputation: 2115
I have a setup in which an actor(A) creates an actor(B) which it supervises and restarts when it crashes. From outside the actor system I want to get some value from the 'nested' actor B.
I can directly ask the actorSystem for the nested actor using:
val nestedActorB = myActorSystem.actorFor("/user/A/B")
Can I keep a reference to this actor or should I ask the actorSystem for the ref each time I want to ask the nested actor something. (Or should I ask the parent actor for the ref)?
What would be a best practice in this scenario? (scala 2.9.1 and akka 2.0.4)
Thanks, Albert
Upvotes: 1
Views: 291
Reputation: 1019
Yes you can keep a reference to the actor and not ask the system every time with actorFor
.
There is a difference between local and remote actors and the documentation passage you are referring to is ambiguous and I've opened a ticket.
A local actor is only looked up once during the actorFor
call so it needs to exist before you look it up, while sends to a remote actor are always by path and the remote actor is looked up on the remote node every time.
Upvotes: 2
Reputation: 3869
As you can see in the akka doc, a reference is an object that, at runtime, traverses the actor tree every time you send a message or check the liveliness of the actor you are referring to.
This means you can use the reference you obtain with the actorFor method any time you want to communicate with that actor, even if the actor belonging to that path is restarted or even if it's already terminated.
Upvotes: 1
Reputation: 16859
Even after the restart the reference stays the same. B
will always be a child of A
so the "/A/B"
path is stable. If A
is a top level actor, you will always find him under"/user/A"
. So yeah, "/user/A/B"
will always point to the same actor.
Upvotes: 1