Akka actorSelection vs actorOf Difference

2.6k views Asked by At

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?

3

There are 3 answers

3
Piotr Rudnicki On BEST ANSWER

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.

0
V. S. On

Here is a brief summary of ActorOf vs. ActorSelection; I hope it helps:

https://getakka.net/articles/concepts/addressing.html

Actor references may be looked up using the ActorSystem.ActorSelection method. The selection can be used for communicating with said actor and the actor corresponding to the selection is looked up when delivering each message.

In addition to ActorSystem.actorSelection there is also ActorContext.ActorSelection, which is available inside any actor as Context.ActorSelection. This yields an actor selection much like its twin on ActorSystem, but instead of looking up the path starting from the root of the actor tree it starts out on the current actor.

Summary: ActorOf vs. ActorSelection

ActorOf only ever creates a new actor, and it creates it as a direct child of the context on which this method is invoked (which may be any actor or actor system). ActorSelection only ever looks up existing actors when messages are delivered, i.e. does not create actors, or verify existence of actors when the selection is created.

0
Orar On

In either processes, there is an associated cost of producing an ActorRef. Creating user top level actors with system.actorOf cost a lot as it has to deal with error kernel initialization which also cost significantly. Creating ActorRef from child actor is very fair making it suitable for one actor per task design. If in an application, for every request, a new set of actors are created without cleanup, your app may run out of memory although akka actors are cheap. Another good is actorOf is immediate as you mentioned.

In abstract terms, actorSelection with resolveOne looks up the actor tree and produces an actorRef in a future as is not so immediate especially on remote systems. But it enforces re-usability. Futures abstract the waiting time of resolving an ActorRef.