简体   繁体   中英

How can I guarantee a pooled AKKA actor only sends and receives from it's own child actor?

I have several actors in a pool. I want each of these actors to create child actor. The problem is that when I create the child actor and save the reference, I never know which child is going to get messages the pooled actor sends to it. The pool was created as a smallestMailBox pool. In the constructor of the pooled actor I do:

documentReaderActor = this.getContext().actorOf(Props.create(DocumentReaderActor.class ));

This is it's child. I do some pattern.asks and some tells, but they don't end up on the same child. First I do a:

documentReaderActor.tell(startReading, getSelf());

later I do over and over:

     Future<Object> future = Patterns.ask(documentReaderActor, adSend,
                timeout);
     adReceive = (AkkaDocument) Await.result(future, timeout.duration());

(adReceive and adSend are a AkkaDocument class object)

When I am all done I send:

documentReaderActor.tell(documentDone, getSelf());

So I have 10 of these pooled actors each creating a child actor but the messages don't go to the same child each time. How can I guarantee the pooled actor only talks to it's child?

The way Akka's SmallestMailBox router works is that it will redirect any message sent to its ActorRef to one of the routees based on the following rules in the order shown:

  • pick any idle routee (not processing message) with empty mailbox
  • pick any routee with empty mailbox
  • pick routee with fewest pending messages in mailbox
  • pick any remote routee, remote actors are consider lowest priority, since their mailbox size is unknown

What this means from your perspective is that you can't guarantee that the response to one of your Asks will end up at the same actor that created it.

If you want to use a pool you may need to roll your own Custom Router .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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