简体   繁体   English

如何在 Akka Classic 中向演员的父母发送消息?

[英]How to send a message to an actor's parent in Akka Classic?

What is the method for an actor to send a message to its parent?演员向其父级发送消息的方法是什么?

I'm using Akka 2.2我正在使用 Akka 2.2

You are looking for你正在寻找

getContext().parent()

which gives you the ActorRef of the parent, so you can do它为您提供了父级的 ActorRef,因此您可以执行

getContext().parent().tell(...)

With Akka 2.4, you have to do context.parent inside an actor to have its parent actor reference.在 Akka 2.4 中,您必须在 actor 内执行context.parent以获取其父 actor 引用。 After that, you can send it a message as before ( context.parent ! "hello" ).之后,您可以像以前一样向它发送消息( context.parent ! "hello" )。

It should be noted, eg for anybody who comes to this question through a search, that Akka 2.6 introduces a typed API for interacting with and defining actors and that the docs starting in version 2.6 guide towards using the typed API.应该注意的是,例如,对于通过搜索遇到这个问题的任何人,Akka 2.6 引入了一个类型化 API,用于与参与者交互和定义参与者,并且从 2.6 版开始的文档指导使用类型化 API。

In an actor defined using the typed API, the ActorContext no longer directly provides an ActorRef to the parent actor, so the answers above won't work.在使用类型化 API 定义的 actor 中, ActorContext不再直接向父 actor 提供ActorRef ,因此上面的答案将不起作用。 See this question for a (Scala) solution in Akka Typed.请参阅此问题以了解 Akka Typed 中的 (Scala) 解决方案。 The analogous Java API would be:类似的 Java API 将是:

import akka.actor.typed.javadsl.Adapter

ActorRef parent = Adapter.toClassic(getContext()).parent()

Note that this is an untyped ActorRef : you can send any message to the parent actor, but the parent actor is not under any obligation to accept the message you send.请注意,这是一个无类型的ActorRef :您可以向父 actor 发送任何消息,但父 actor 没有任何义务接受您发送的消息。

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

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