Reputation: 15385
So seems I have two possibilities to get hold of a child actor instance:
context.actorSelection
, which returns a Future[ActorRef]
context.actorSelection(actorNameString).resolveOne(2.seconds)
context.child
, which returns an Option[ActorRef]
context.child(actorNameString)
So which one should I prefer and why?
I know what using actorSelection
, I can be async, but what other reasons exist to favour one over the other?
Upvotes: 0
Views: 622
Reputation: 3514
Unless you use remote deployment for your child actors (in which case I wouldn't know what to answer), or you want to get a reference to the child of a child, I don't think you should use get an ActorRef
via context.actorSelection
to get references to child actors.
context.actorSelection
is meant to identify and get references of (multiple) actors running anywhere (on other JVMs/hosts) and that's why it is asynchronous. Sure you can use it to get a hold of a child actor, but if you can go for context.child
.
I think that you can see context.child
as a specialized version (of context.actorSelection(actorNameString).resolve
) for cases where you want to get a single reference to a child actor.
Upvotes: 1