简体   繁体   English

Akka演员消息序列

[英]Akka actor message sequence

I have the following actor structure 我有以下演员结构

import akka.actor.ActorRef;
import akka.actor.UntypedActor;

public class ExampleActor extends UntypedActor {
    ActorRef worker1;
    ActorRef worker2;

    @Override
public void onReceive(Object msg) throws Exception {


    if (msg instanceof PerformTask) {

        worker1.tell(doTask1, getSelf());
        worker2.tell(doTask2, getSelf());

        // Perform some task with the results of the above two calls

    }
}

} }

import akka.actor.UntypedActor;

public class Worker1 extends UntypedActor {

    @Override
    public void onReceive(Object msg) throws Exception {

        if (msg instanceof doTask1) {
            return result1;
        }

    }

}

Worker 2 工人2

import akka.actor.UntypedActor;

public class Worker2 extends UntypedActor {

    @Override
    public void onReceive(Object msg) throws Exception {

        if (msg instanceof doTask2) {
            return result2;
        }

    }

}

How do I make sure that result 1 and result 2 are obtained before ExampleActor performs its own task? 在ExampleActor执行自己的任务之前,如何确保获得结果1和结果2?

If I have separate messages for receieving the response in exampleActor, how do I pass this response to "PefrformTask" action? 如果在exampleActor中有用于接收响应的单独消息,如何将响应传递给“ PefrformTask”操作?

You need to start building a state in your ExampleActor . 您需要开始在ExampleActor建立状态。 When you receive any of the two responses you are waiting for from your Workers , you need save that response. 当您收到来自Workers的两个答复中的任何一个时,您需要保存该答复。 Either the message, or just the relevant content of it. 消息或消息的相关内容。 Something like: 就像是:

if (msg instanceof WorkerTaskResult) {
    results.add((WorkerTaskResult)msg);
}
if(results.size() == number_of_results_expected) {
    //perform whatever I wanted to do on those results from workers
}

results is of course non-static field on your ExampleActor . results当然是您的ExampleActor上的非静态字段。

Note: 注意:
This example above is super-simplistic just to give you the idea about the solution. 上面的示例非常简单,只是为您提供有关解决方案的想法。 There are many things to watch out for: ExampleActor restarted / Workers not producing results, etc. 有许多注意事项:ExampleActor重新启动/工人未产生结果,等等。

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

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