Search code examples
scalaakkaactor

Akka actorSelection vs actorOf Difference


Is there a difference between these two? When I do:

context.actorSelection(actorNameString)

I get an ActorSelection reference which I can resolve using the resolveOne and I get back a Future[ActorRef]. But with an actorOf, I get an ActorRef immediately. Is there any other vital differences other than this?

What might be the use cases where in I would like to have the ActorRef wrapped in a Future?


Solution

  • actorOf is used to create new actors by supplying their Props objects.

    actorSelection is a "pointer" to a path in actor tree. By using resolveOne you will get actorRef of already existing actor under that path - but that actorRef takes time to resolve, hence the Future.

    Here's more detailed explanation: http://doc.akka.io/docs/akka/snapshot/general/addressing.html

    An actor reference designates a single actor and the life-cycle of the reference matches that actor’s life-cycle; an actor path represents a name which may or may not be inhabited by an actor and the path itself does not have a life-cycle, it never becomes invalid. You can create an actor path without creating an actor, but you cannot create an actor reference without creating corresponding actor.