Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

If I create an actor using context().actorOf() in Akka, I get back a valid ActorRef. However, if I do the same but create an ActorRef using actorFor and the path I know that the actor will appear at, I do not reliably get a valid ActorRef back. How can I tell that an actor has been registered successfully?

In the description above, I could simply use the ActorRef returned from actorOf(). However, in my actual case I create an actor that itself registers a child actor and I need to resolve that, so the problem in general is "how can I wait / register to be informed of an actor having been registered at a known path?".

like image 561
SoftMemes Avatar asked May 11 '12 09:05

SoftMemes


People also ask

How do Actors work in Akka?

What is an Actor in Akka? An actor is essentially nothing more than an object that receives messages and takes actions to handle them. It is decoupled from the source of the message and its only responsibility is to properly recognize the type of message it has received and take action accordingly.

How can I send a message to an actor in Akka?

1) Akka Actor tell() Method It works on "fire-forget" approach. You can also use ! (bang) exclamation mark to send message. This is the preferred way of sending messages.

Which method provides initial Behaviour to actor?

Start Hook This method is called when the actor is first created.

Can Akka Actors stop other Actors?

In Akka, you can stop Actors by invoking the stop() method of either ActorContext or ActorSystem class. ActorContext is used to stop child actor and ActorSystem is used to stop top level Actor. The actual termination of the actor is performed asynchronously.


1 Answers

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

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

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

Resolve the ActorRef matching this selection. The result is returned as a Future that is completed with the ActorRef if such an actor exists. It is completed with failure ActorNotFound if no such actor exists or the identification didn't complete within the supplied timeout.

Under the hood it talks to the actor to verify its existence and acquire its ActorRef.

like image 99
Andrejs Avatar answered Sep 30 '22 13:09

Andrejs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!