문제

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? .

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top