Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three ways to know existence of an akka actor

Tags:

java

akka

I am working on akka actors(JAVA) and recently came to know that there are 3 ways(may be more) to know existing of an actor.

  1. Sending a Identify message:

    ActorSelection sel = actorSystem.actorSelection("akka://test/user/TestActor");
    AskableActorSelection asker = new AskableActorSelection(sel);
    Future<Object> future = asker.ask(new Identify(1), new Timeout(5,
            TimeUnit.SECONDS));
    ActorIdentity identity = (ActorIdentity) Await.result(future, timeOut.duration());
    ActorRef reference = identity.getRef();
    if(reference != null){
      // Actor exists
    } else {
    // Actor does not exits
    }
    
  2. resolveOne method:

    ActorSelection sel = actorSystem.actorSelection("akka://test/user/TestActor");
    Future<ActorRef> future = sel.resolveOne(new Timeout(5,
            TimeUnit.SECONDS));
    // Wait for the completion of task to be completed.
    future.onComplete(new OnComplete<ActorRef>() {
        @Override
        public void onComplete(Throwable excp, ActorRef child)
                throws Throwable {
            // ActorNotFound will be the Throwable if actor not exists
            if (excp != null) {
                    // Actor does not exists
            } else {
                // Actor exits
            }
        }
    }, actorSystem.dispatcher());
    
  3. DeatchWatch: Create another actor call getContext().watch(ActorRef of actorToWatch); and check for receive of Terminated message. This may be used only on already created actor.

1,2 tells existence of actor and 3 monitors. I would like to know the use cases of these three and their effects on actors mail boxes and functionalities, so that i can choose the type which will be apt for my application.

Is Checking for existence of an actor is a good practice? If not why? .

like image 505
achuth Avatar asked Apr 28 '14 03:04

achuth


People also ask

What is actor system 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.

What is actor in play framework?

An actor system manages the resources it is configured to use in order to run the actors which it contains. A Play application defines a special actor system to be used by the application. This actor system follows the application life-cycle and restarts automatically when the application restarts.

How do you make a child actor Akka?

You can create child actor by using implicit context reference. ActorSystem is used to create root-level or top-level actor. Akka provides you context so that you can create child actor also. In the following example, we have created a child actor by using context reference.


1 Answers

Well, there is only one way to know whether an Actor existed at a certain point in the past: if you receive a message from it. All of the above are just variations on this theme.

That said, once you have the ActorRef, you can use DeathWatch to be notified about that actor’s termination. But not yet having received the Terminated message does not mean that the actor is still alive: the Terminated might already be on its way.

Think of Actors as people who can only communicate by sending email. That analogy works quite well for the semantics of their interaction.

like image 176
Roland Kuhn Avatar answered Sep 29 '22 13:09

Roland Kuhn