向父级发送消息以及等待子级的响应结果

3
我正在尝试从父级Actor向子Actor发送消息,但是该消息直到父Actor完成onReceive块后才到达。 是否有一种方法可以向子Actor发送消息并在父Actor上等待结果。
Parent:
onReveive(message) {
    if(message instanceof ChildOrder) {
        onChildOrder(message);
    }
}

onChildOrder(message) {
    Future<Object> future = Patterns.ask(childActorRef, order, timeout);
    Object result = Await.result(future, timeout.duration()); /* this always times out */ 
}

Child:
onReveive(message) {
    do stuff
}

如上所述,在onChildOrder方法中,对象result = Await.result(future, timeout.duration())总是超时,并且当父级的onReceive方法完成时,消息ChildOrder到达子级的onReceive方法。
似乎子级在父级完成之前无法处理任何消息。
是否有办法发送消息给子级并等待结果?
1个回答

0
你需要让子进程将结果发送回父进程,否则父进程会永远等待答案。
在子进程中:
@Override
public void onReceive(Object message) throws Exception {
    MyResult res = doStuff(message);
    // send result back
    getSender().tell(res, getSelf());
}

Patterns.ask的文档中指出:

[...] 这意味着目标 actor 需要将结果发送到提供的发送者引用。


祖父给孙子的转发信息:

如果您需要孩子回复祖父母,父母可以将消息转发给孩子,这样对于孩子来说,发送者是祖父母,因此可以绕过父母而直接回复祖父母:

在父母端:

@Override
public void onReceive(Object message) throws Exception {
    if(message instance of MessageFromGrandParent) {
        child.forward(message, getContext());
    }
    // ...
}

是的,这条消息来自祖先Actor,所以我希望在某个层面上削减异步调用并一次性收集结果,并将它们发送回来。再次感谢,现在我需要更改一些东西 :) - whb
在这种情况下,你可能想要“转发”这条消息。请检查我的编辑。 - Jean Logeart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接