简体   繁体   English

java-akka:将消息发送回祖父母

[英]java-akka : sending back message to grandparent

I have created 3 levels of actors. 我创建了3个级别的演员。 Level 1 is the supervisor actor. 1级是主管演员。 Supervisor actor will trigger message to Level2 actor and will also take action based on the responses back from the child actors (Level1 and Level2) based on the type of message received. 主管参与者将触发消息发送给Level2参与者,并且还将根据收到的消息类型根据子参与者(Level1和Level2)返回的响应采取措施。 Below is the code of supervisor 下面是主管的代码

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);
}

In Level 2 actor, the work is delegated to some Processor. 在2级演员中,工作被委派给某个处理器。 This processor will spawn another actor (Level3 actor). 该处理器将生成另一个actor(Level3 actor)。 Now, I need to return the response of taskcomplete from Level3 actor back to Level1 actor (supervisor actor) for it to take action based on the response. 现在,我需要将taskcomplete的响应从Level3 actor返回给Level1 actor(主管actor),以便它根据响应采取行动。

Below is the sample code of Level2 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);
}

How can I send the response back from 3rd level actor (child) to grandparent (1st level). 如何将响应从3级演员(孩子)发回祖父母(1级)。

In your code snippet for Level2 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);
}

You have the line ActorRef parent = getContext().parent();// This gives me the parent(Level2) which gives you the ActorRef for Level2's parent ie Level1, which is the grandparent of Level3. 您有一行ActorRef parent = getContext().parent();// This gives me the parent(Level2) ,它为您提供了Level2父级的ActorRef,即Level1,它是Level3的祖父母。

So if you want to tell Level1 that Level3 has completed then in Level2 actor you can either use: 因此,如果您想告诉Level1 Level3已经完成,则可以在Level2 actor中使用:

parent.tell(message, getSelf());

which will pass the Level3ActorTaskCompleteMsg on to Level1 and the sender will be Level2 or you could use: 它将把Level3ActorTaskCompleteMsg传递给Level1,发送者将是Level2,或者您可以使用:

parent.forward(message, getContext());

which will forward the Level3ActorTaskCompleteMsg to Level1 and the sender when Level1 will be Level3 ie it's grandchild. 当Level1将是Level3(即其孙子)时,它将把Level3ActorTaskCompleteMsg给Level1,并向发送方转发。

Either way you will have achieved the goal of sending the Level3ActorTaskCompleteMsg from Level3 to it's grandparent at Level1. 无论哪种方式,您都可以实现将Level3ActorTaskCompleteMsg从Level3发送到Level1的祖父母的目标。

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

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

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