繁体   English   中英

在父母上向子actor发送消息并等待结果

[英]Sending message to child actor on parent and wait result

我正在尝试从父级向子actor发送消息,但消息不会到达,直到父actor完成onReceive块。 是以任何方式向子节点发送消息并在父节点上等待结果。

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
}

如上所述,Object result = Await.result(future,timeout.duration()); onChildOrder方法上的行总是超时,当父级的onReceive方法完成消息时,ChildOrder到达子节点的onReceive方法。

如果没有父级先完成,孩子似乎无法处理任何消息。

有没有办法向孩子发送消息并等待结果。

您需要孩子将结果发送回父母,否则父母会一直等待答案。

在孩子:

@Override
public void onReceive(Object message) throws Exception {
    MyResult res = doStuff(message);
    // send result back
    getSender().tell(res, getSelf());
}

它在Patterns.ask的文档中指出:

[...]这意味着目标参与者需要将结果发送给提供的发件人参考。


根据评论进行编辑:

从祖父到孩子的转发信息:

如果您需要将孩子发送回祖父母,父母可以将该消息转发给孩子,以便孩子的发件人是祖父母,因此在回程途中绕过父母:

在父母:

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

暂无
暂无

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

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