joesan
joesan

Reputation: 15385

Akka Actor Selection vs Context child

So seems I have two possibilities to get hold of a child actor instance:

  1. By using context.actorSelection, which returns a Future[ActorRef]
context.actorSelection(actorNameString).resolveOne(2.seconds)
  1. By using 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

Answers (1)

Frederic A.
Frederic A.

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

Related Questions