简体   繁体   English

我如何获得演员系统中的所有演员

[英]How do I get all actors in an actor system

I have a actor system.我有一个演员系统。 I would like to log (for testing) all the actors (addresses/paths) in the system我想记录(用于测试)系统中的所有参与者(地址/路径)

I tried calling我试着打电话

ActorSelection actorSelection = context().actorSelection("akka://*");
System.out.println(Arrays.asList(actorSelection.path()));

inside one of the root actors.在根演员之一里面。 It prints它打印

[akka:, ^\Q\E.*\Q\E$]

But for fact I know there are more actors in my system and working.但事实上,我知道我的系统中有更多的演员在工作。 Whats the proper way to get this list?获取此列表的正确方法是什么?

If it's for test purposes, I would create a dedicated RetrieveActors actor with this behavior (akka v2.4.17 syntax):如果是为了测试目的,我会创建一个具有这种行为的专用RetrieveActors actor(akka v2.4.17语法):

import akka.actor.*;
import akka.event.Logging;
import akka.event.LoggingAdapter;

public class RetrieveActors extends UntypedActor {
    private LoggingAdapter log = Logging.getLogger(getContext().system(), this);
    private String identifyId;

       public RetrieveActors(String identifyId) {
           this.identifyId = identifyId;
       }

       @Override
       public void preStart() {
           ActorSelection selection = getContext().actorSelection("/*");
           selection.tell(new Identify(identifyId), getSelf());
       }

       @Override
       public void onReceive(Object message) throws Throwable {
           if (message instanceof ActorIdentity) {
               ActorIdentity identity = (ActorIdentity) message;

               if (identity.correlationId().equals(identifyId)) {
                   ActorRef ref = identity.getRef();

                   if (ref != null) { // to avoid NullPointerExceptions
                       // Log or store the identity of the actor who replied
                       log.info("The actor " + ref.path().toString() + " exists and has replied!");

                       // We want to discover all children of the received actor (recursive traversal)
                       ActorSelection selection = getContext().actorSelection(ref.path().toString() + "/*");
                       selection.tell(new Identify(identifyId), getSelf());
                   }

               }

           }
       }
}

And then when you want to list all your actors created, just create a new instance of RetrieveActors :然后当你想列出你创建的所有演员时,只需创建一个新的RetrieveActors实例:

// "1" is the identifyId to make your retrieval calls unique to this actor instance
system.actorOf(Props.create(RetrieveActors.class, "1"), "LookupActor1");

Note that some actors may be quite long to answer if they are busy since this pattern use normal messages and mailboxes.请注意,如果某些 actor 很忙,他们可能需要很长时间才能回答,因为此模式使用普通消息和邮箱。 Also, the delivery may be not guaranteed.此外,可能无法保证交货。 You will retrieve only the actors that have been already created.您将仅检索已创建的演员。 More info here .更多信息在这里

If you need to get several times all actors in your akka system, I suggest you to modify the RetrieveActors behavior to perform actor retrieval in reaction of some custom message instead of doing it at initialization.如果您需要多次获取 akka 系统中的所有演员,我建议您修改RetrieveActors行为以执行演员检索以响应某些自定义消息,而不是在初始化时执行。 This may avoid you to create several instances of the same Actor.这可以避免您创建同一个 Actor 的多个实例。

Hope it helps.希望能帮助到你。

ActorSystem has opened printTree method, so use something similar to ActorSystem 开启了 printTree 方法,所以使用类似于

   ActorSystem<Object> actors = ActorSystem.create(...);
   System.out.println(actors.printTree());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM