java-akka:向祖父母发送消息

时间:2015-07-14 09:35:20

标签: java akka

我创造了3个级别的演员。 1级是主管演员。主管演员将触发消息给Level2演员,并且还将根据收到的消息类型,根据子演员(Level1和Level2)的响应采取行动。 以下是主管的代码

if (message instanceof OutBoundDataExportDTO) {
    // This to trigger query processor

    OutBoundDataExportDTO data = OutBoundDataExportDTO.class.cast(message);
    LOGGER.info("Received:  " + data.toString());
    ActorRef ruleInstExportActor = actorSystem
        .actorOf(Props.create(OutboundsQueryActor.class, applicationContext)
        .withRouter(new RoundRobinPool(5)));
    ruleInstExportActor.tell(data, getSelf());

} else if (message instanceof TaskCompleteMsg) {
    TaskCompleteMsg taskCompleteMsg= TaskCompleteMsg.class.cast(message);
    taskService.update(taskCompleteMsg);
}

在Level 2 actor中,作品被委托给某个处理器。这个处理器将产生另一个actor(Level3 actor)。现在,我需要将来自Level3 actor的taskcomplete的响应返回给Level1 actor(主管演员),以便它根据响应采取行动。

以下是Level2 actor的示例代码:

if (message instanceof Level1ActorMessage) {
     OutboundQueryProcessor queryProcessor =  queryProcessorFactory
         .getQueryProcessor(queryName);
     response = queryProcessor.executeQuery(templateId, queryData, task);

} else if (message instanceof Level3ActorTaskCompleteMsg) { // This is the response from Level 3 actor, once it has completed 
    ActorRef parent = getContext().parent(); // This gives me the   parent(Level2)
    // How do i send it back to its parent
    // parent.tell(returnResponse, parent);
}

如何将回复从第3级演员(孩子)发送回祖父母(第1级)。

1 个答案:

答案 0 :(得分:0)

在Level2 actor的代码片段中:

if (message instanceof Level1ActorMessage) {
    OutboundQueryProcessor queryProcessor = queryProcessorFactory.getQueryProcessor(queryName);
    response = queryProcessor.executeQuery(templateId, queryData, task);

} else if(message instanceof Level3ActorTaskCompleteMsg){// This is the response from Level 3 actor, once it has completed 
    ActorRef parent = getContext().parent();// This gives me the   parent(Level2)
    //How do i send it back to its parent
    //   parent.tell(returnResponse, parent);
}

你有一行ActorRef parent = getContext().parent();// This gives me the parent(Level2)给你Level2的父亲的ActorRef,即Level1,它是Level3的祖父母。

因此,如果你想告诉Level1 Level3已经完成,那么在Level2 actor中你可以使用:

parent.tell(message, getSelf());

Level3ActorTaskCompleteMsg传递给Level1,发件人将为Level2,或者您可以使用:

parent.forward(message, getContext());

当Level1为Level3即它的孙子时,会将Level3ActorTaskCompleteMsg转发给Level1和发送者。

无论哪种方式,您都可以达到将Level Level3ActorTaskCompleteMsg从Level3发送给Level1的祖父母的目标。

请参阅http://doc.akka.io/docs/akka/snapshot/java/untyped-actors.html#Forward_message