In Akka, how do I know when an actor is ready to use after registering with actorOf ()?

If I create an actor using context (). actorOf () in Akka, I return a valid ActorRef. However, if I do the same, but create an ActorRef using actorFor, and the way that I know the actor will appear, I cannot reliably return a valid ActorRef. How can I say that an actor is successfully registered?

In the above description, I could just use the ActorRef returned from actorOf (). However, in my actual case, I create an actor who registers the child actor himself, and I need to resolve it, so the problem as a whole is “how can I wait / register in order to be informed that the actor was registered in a known way ? "

+5
source share
2 answers

First of all, there is no guarantee that the ActorRef that you return from actorOf is still alive, as its constructor could fail.

Secondly, actorFor can find an actor, but he died right after he was found and just before you started working with him.

Thirdly, it is usually good practice to structure your application so that dependencies are distributed logically, so there is a natural rendez-vous point between your actors.

Hope any of the above help,

happy hAkking!

+1
source

Since Akka 2.2.1, you can use ActorSelection.resolveOne to get the ActorRef from the actor selection:

implicit val timeout = Timeout(5, TimeUnit.SECONDS)
val selection = system.actorSelection("/user/myActor")
val actor = Await.result(selection.resolveOne(), timeout.duration)

http://doc.akka.io/api/akka/2.2.1/index.html#akka.actor.ActorSelection

ActorRef, . , ActorRef , . ActorNotFound, -.

, ActorRef.

+7

All Articles