我创建了3个级别的演员。
1级是主管演员。主管参与者将触发消息发送给Level 2参与者,并且还将根据收到的消息类型根据子参与者(Level 1和Level 2)返回的响应采取措施。
下面是主管的代码
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);
}
在2级演员中,工作被委派给某个处理器。该处理器将生成另一个actor(Level3 actor)。现在,我需要将Taskcomplete的响应从Level3 actor返回给Level1 actor(主管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级)。
最佳答案
在您的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父级(即Level1的祖父母)的ActorRef。因此,如果您想告诉Level1 Level3已经完成,则可以在Level2 actor中使用:
parent.tell(message, getSelf());
它将
Level3ActorTaskCompleteMsg
传递给Level1,发件人将为Level2,或者您可以使用:parent.forward(message, getContext());
它将
Level3ActorTaskCompleteMsg
转发到Level1,并且当Level1为Level3(即其孙子)时将发件人转发。无论哪种方式,您都可以实现将
Level3ActorTaskCompleteMsg
从Level3发送到Level1的祖父母的目标。见http://doc.akka.io/docs/akka/snapshot/java/untyped-actors.html#Forward_message
关于java - java-akka:将消息发送回祖 parent ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31402936/