How does ActorSelection in Akka work?

How does ActorSelection Akka work?

This topic is based on a conversation between an anonymous user of Akka and our “Ask AI about Akka” bot in the documentation.

ActorSelection is a feature in Akka that allows you to obtain a reference to an actor by specifying its path. This can be useful when you need to communicate with an actor but don’t have its ActorRef.
In Akka, each actor has a unique logical path, which is obtained by following the chain of actors from child to parent until reaching the root of the actor system. ActorSelection uses these paths to look up actors. You can specify absolute or relative paths—logical or physical—and receive back an ActorSelection with the result. For example:

// Scala
// will look up this absolute path
context.actorSelection("/user/serviceA/aggregator")
// will look up sibling beneath same supervisor
context.actorSelection("../joe")
// Java
// will look up this absolute path
getContext().actorSelection("/user/serviceA/actor");
// will look up sibling beneath same supervisor
getContext().actorSelection("../joe");

However, it’s important to note that it’s generally preferable to communicate with other actors using their ActorRef instead of relying upon ActorSelection. Exceptions to this are when sending messages using the At-Least-Once Delivery facility or initiating the first contact with a remote system.
ActorSelection can also be used to interact with actors on a remote node. For example:

// Scala
val selection = context.actorSelection("akka://[email protected]:25520/user/actorName")
selection ! "Pretty awesome feature"
// Java
ActorSelection selection = context.actorSelection("akka://[email protected]:25520/user/actorName");
selection.tell("Pretty awesome feature", getSelf());

To acquire an ActorRef for an ActorSelection, you can send an Identify message to the ActorSelection, which will be replied to with an ActorIdentity message containing the ActorRef.

Please note that ActorSelection isn’t supported in Akka Typed. Instead, the Receptionist is used for finding actors by a registered key.
Sources:

How does actorSelection work in Akka Typed?

In Akka Typed, ActorSelection is not supported. Instead, the Receptionist is used for finding actors by a registered key. This is a change from Akka Classic, where ActorSelection was used to send messages to a path without having an ActorRef of the destination.
In Akka Typed, actors are started as children of the user guardian actor or children of other actors in the actor hierarchy. If you need to spawn outside of this guardian, you can use the SpawnProtocol to spawn as needed.
Here is an example of how you might use the Receptionist in Akka Typed:

// Scala
val receptionist: ActorRef[Receptionist.Command] = ???
val listingResponseAdapter: ActorRef[Receptionist.Listing] = ???
receptionist ! Receptionist.Find(MyServiceKey, listingResponseAdapter)
// Java
ActorRef<Receptionist.Command> receptionist = ???
ActorRef<Receptionist.Listing> listingResponseAdapter = ???
receptionist.tell(Receptionist.find(MyServiceKey, listingResponseAdapter));

In these examples, MyServiceKey is the key that the actor you’re looking for is registered under.

For more information, please refer to the Akka documentation on Actor Discovery.

Source: Learning Akka Typed from Classic