【问题标题】:Three ways to know existence of an akka actor了解 akka 演员存在的三种方法
【发布时间】:2014-04-28 03:16:01
【问题描述】:

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

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

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

检查演员的存在是一种好习惯吗?如果不是,为什么? .

【问题讨论】:

  • 一般来说,需要知道演员是否存在是 Akka 代码库中的代码异味。
  • 在您的第一个示例中,变量timeOut 指的是什么?
  • timeOut 是等待响应的时间(Timeout timeOut = new Timeout(5, TimeUnit.SECONDS))

标签: java akka


【解决方案1】:

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

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

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

【讨论】:

  • 感谢@Roland Kuhn “只有一种方法可以知道某个演员在过去的某个时间点是否存在””
  • 我认为上面的每一个功能都不需要向actor发送消息,所以最后上面所有的工作都只是向actor发送和接收消息。
猜你喜欢
  • 2019-05-10
  • 1970-01-01
  • 2016-03-07
  • 1970-01-01
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 2013-08-03
  • 2019-04-23
相关资源
最近更新 更多