1
votes

I have created 3 levels of actors. Level 1 is the supervisor actor. 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. 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. This processor will spawn another 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.

Below is the sample code of 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).

1
Please post more detailed questions here - tarzanbappa
Please provide the relevant codebase so that it is easier to understand what is going on. Also, what have you tried in the same regard? - daydreamer

1 Answers

0
votes

In your code snippet for 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 i.e. Level1, which is the grandparent of Level3.

So if you want to tell Level1 that Level3 has completed then in Level2 actor you can either use:

parent.tell(message, getSelf());

which will pass the Level3ActorTaskCompleteMsg on to Level1 and the sender will be Level2 or you could use:

parent.forward(message, getContext());

which will forward the Level3ActorTaskCompleteMsg to Level1 and the sender when Level1 will be Level3 i.e. it's grandchild.

Either way you will have achieved the goal of sending the Level3ActorTaskCompleteMsg from Level3 to it's grandparent at Level1.

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