对context.parent
我来说,参考没有这样做。在 Play 项目中,我启动了一个新的 Actor 来处理每个传入的请求:
val myActor: ActorRef = system.actorOf(Props(classOf[MyActor])
val future: Future[String] = (myActor ? Hello)(5.seconds).mapTo[String]
future.map(result => Ok(result)).recover {
case ex: AskTimeoutException => Ok("timeout expired")
case _ => Ok("error")
}
这会在 : 的接收方法中触发更多的actor消息传递myActor
:当anotherActor
响应 时,后者会向其父级myActor
发送消息。AllGood
class MyActor extends Actor {
// other stuff, among which setup of anotherActor
def receive: Actor.Receive = {
case Hallo => anotherActor ! Hello
case HelloBack => context.parent ! AllGood
}
}
class AnotherActor extends Actor {
def receive: Actor.Receive = {
case Hallo => sender ! HelloBack
}
}
一切正常,直到myActor
尝试向context.parent
. 我deadLetters
在控制台中收到一条消息。
from Actor[xyz] to Actor[akka://my-system/user] was not delivered. [1] dead letters encountered.
myActor
如果保留对 original 的引用,我可以使它工作sender
,并且代码看起来像这样:
class MyActor extends Actor {
var myDad: Option[ActorRef] = None // <--- NEW REFERENCE
// other stuff, among which setup of anotherActor
def receive: Actor.Receive = {
case Hallo => {
myDad = Some(sender)
anotherActor ! Hello
}
case HelloBack => myDad ! AllGood <-- MYDAD INSTEAD OF CONTEXT.PARENT
}
}
class AnotherActor extends Actor {
def receive: Actor.Receive = {
case Hallo => sender ! HelloBack
}
}
引用myDad
实际上类似于Actor[akka://my-system/temp/$a]
,而我希望它与context.parent
which was相同Actor[akka://my-system/user]
。不是同一个演员吗?
任何人都可以向我解释这种行为,并提出一种更清洁的方法来解决这个问题吗?谢谢。