Java 了解阿克卡演员存在的三种方法

Java 了解阿克卡演员存在的三种方法,java,akka,Java,Akka,我正在研究akka actors(JAVA),最近我知道有3种方法(可能更多)来了解一个演员的存在 发送识别信息: ActorSelection sel = actorSystem.actorSelection("akka://test/user/TestActor"); AskableActorSelection asker = new AskableActorSelection(sel); Future<Object> future = asker.ask(new Identif

我正在研究akka actors(JAVA),最近我知道有3种方法(可能更多)来了解一个演员的存在

  • 发送识别信息

    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
    }
    
    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());
    
    ActorSelection sel=actorSystem.ActorSelection(“akka://test/user/TestActor");
    AskableActorSelection asker=新AskableActorSelection(sel);
    Future-Future=asker.ask(新标识(1)、新超时(5、,
    时间单位(秒);
    ActorIdentity标识=(ActorIdentity)wait.result(future,timeOut.duration());
    ActorRef reference=identity.getRef();
    如果(引用!=null){
    //演员存在
    }否则{
    //演员不退出
    }
    
  • 解决一种方法

    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
    }
    
    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());
    
    ActorSelection sel=actorSystem.ActorSelection(“akka://test/user/TestActor");
    Future Future=sel.resolveOne(新超时(5,
    时间单位(秒);
    //等待完成要完成的任务。
    future.onComplete(新的onComplete(){
    @凌驾
    未完成的公共无效(可抛弃的例外情况、行为人或儿童)
    扔掉的{
    //如果actor不存在,则ActorNotFound将是可丢弃的
    如果(excp!=null){
    //参与者不存在
    }否则{
    //演员退出
    }
    }
    },actorSystem.dispatcher());
    
  • 迪奇瓦奇: 创建另一个actor调用getContext().watch(actorwatch的ActorRef)并检查是否接收到已终止的消息。这只能用于已创建的参与者

  • 1,2表示存在参与者和3个监视器。我想知道这三个应用程序的用例以及它们对actors邮箱和功能的影响,以便我可以选择适合我的应用程序的类型


    检查演员是否存在是一种好做法吗?如果不是为什么?

    那么,只有一种方法可以知道某个演员在过去的某个时刻是否存在:如果你从他那里收到消息。以上所有这些都只是这个主题的变体

    也就是说,一旦你有了ActorRef,你就可以使用DeathWatch来获得关于该演员被解雇的通知。但尚未收到终止的消息并不意味着参与者仍然活着:终止的消息可能已经在路上了


    把演员想象成只能通过发送电子邮件进行交流的人。这种类比在他们交互的语义上非常有效。

    通常,需要知道参与者是否存在的是Akka代码库中的代码气味。在第一个示例中,变量
    timeOut
    指的是什么?timeOut是响应的等待时间(timeOut timeOut=新超时(5,TimeUnit.SECONDS)),感谢@Roland Kuhn“只有一种方法可以知道参与者在过去的某个时刻是否存在”“我认为上面的每个功能都可以工作,而不向参与者发送消息,所以最后所有上述工作都只向参与者发送和接收消息。