如何有效地处理其他akka演员的反应

时间:2016-04-28 12:25:28

标签: java akka

考虑一下,

class PageActor extends UntypedActor {
private Page page;
private ActorRef documentActor;
public PDFPageActor(ActorRef pdfDocumentActor) {
    super();
    pdfPage = new PDFPage();
    this.pdfDocumentActor = pdfDocumentActor;
}
@Override
public void onReceive(Object message) throws Exception {
    // handle the message;
}
public void doProcess() {
  statement;
  statement;
  send a request to **Document actor**(requesting a object);
  statement(make use of the above object);
  statement;
  send a request to **Document actor**(requesting a object);        
  statement(make use of above object);
  statement;
  statement(make use of both the objects)
}
}

如果我使用actorRef.tell();如果我们在接下来的后续步骤中需要来自文档演员的响应,那么接下来的步骤之一就是使用模式。但是模式是阻塞的。还有其他更好的解决方法。我经历了其他堆栈溢出答案,但没有得到清晰的图片。

非常感谢任何和所有帮助

1 个答案:

答案 0 :(得分:1)

我会将依赖于“对象”的语句移动到控制/获取此对象的actor或第三个 - “ProcessingActor”。

它可能看起来像这样(方法ar在相应的onReceive上调用)

//in PageActor:
public void doProcess(){
   statement
   documentActor.tell(dataToProcess1, getSelf());
   documentActor.tell(dataToProcess2, getSelf());
}

//called from onReceive when processed data for an object is received
private void onDocumentProcessed(){
   if (all documents processed)
       process final statements
   else
       documents processed++;
}

//////////////////////////////////
//in DocumentActor:
      object = getObject(); //whatever that means in your application (probably blocking op)
      processingActor.forward(Message(object, dataToProcess));

/////////////////////////////////
//in ProcessingActor:
      statement(object); //execute statements that depend on the object
      getSender().tell(result, ActorRef.noSender()); //inform initial actor