知道akka演员存在的三种方法

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

  1. 发送识别消息

    ActorSelection sel = actorSystem.actorSelection("akka://test/user/TestActor"); AskableActorSelection asker = new AskableActorSelection(sel); Future 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 future = sel.resolveOne(new Timeout(5, TimeUnit.SECONDS)); // Wait for the completion of task to be completed. future.onComplete(new OnComplete() { @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(actorToWatch的ActorRef); 并检查是否收到终止消息。 这可能仅用于已创建的actor。

1,2讲述了演员和3名监视器的存在。 我想知道这三个用例及其对演员邮箱和function的影响,以便我可以选择适合我的应用程序的类型。

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

好吧,只有一种方法可以知道一个Actor是否存在于过去的某一点:如果你收到一条消息。 以上所有内容都只是这个主题的变体。

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

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