簡體   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