一个演员如何向其父级发送消息?
我正在使用Akka 2.2
你正在寻找
getContext().parent()
这将给你父级的ActorRef,因此你可以执行
getContext().parent().tell(...)
在Akka 2.4中,要获取一个actor的父actor引用,需要在actor内部使用context.parent
。之后,您可以像以前一样发送消息(context.parent ! "hello"
)。
需要注意的是,对于通过搜索进入此问题的任何人,Akka 2.6引入了一种类型化API,用于与定义演员进行交互,并且从版本2.6开始的文档指导使用类型化API。
在使用类型化API定义的演员中,ActorContext
不再直接提供对父演员的ActorRef
,因此上面的答案将无法工作。请参见this question以获取(Scala)在Akka Typed中的解决方案。类似的Java API将是:
import akka.actor.typed.javadsl.Adapter
ActorRef parent = Adapter.toClassic(getContext()).parent()
ActorRef
:您可以向父级 actor 发送任何消息,但父级 actor 没有义务接受您发送的消息。
tell(...)
epects ActorRef - isn't following construction strange?getContext().parent().tell("message",getContext().parent())
- mprcontext.parent
。 - Ali