gpt4 book ai didi

java - 知道akka actor存在的三种方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:59:59 25 4
gpt4 key购买 nike

我正在研究 akka actors(JAVA),最近了解到有 3 种方法(可能更多)可以了解 actor 的存在。

  1. 发送身份信息:

    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 方法:

    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: 创建另一个 actor 调用 getContext().watch(ActorRef of actorToWatch); 并检查是否收到了 Terminated 消息。这只能用于已创建的 Actor 。

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

检查 Actor 是否存在是一种好做法吗?如果不是,为什么? .

最佳答案

只有一种方法可以知道某个 Actor 在过去的某个时刻是否存在:如果您收到来自它的消息。以上所有只是这个主题的变体。

就是说,一旦您拥有 ActorRef,就可以使用 DeathWatch 来通知该 actor 的终止。但是还没有收到 Terminated 消息并不意味着 actor 还活着:Terminated 可能已经在路上了。

将参与者视为只能通过发送电子邮件进行交流的人。对于他们交互的语义,这个类比非常有效。

关于java - 知道akka actor存在的三种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23331950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com