object SenderTest extends App {
val s = ActorSystem("test")
val ar = s.actorOf(Props[A], "Aactor")
ar ! "abc"
}
class A extends Actor {
val sender_ = sender()
override def receive: Receive = {
case _: String => {
println("inside A receive block")
val b = context.actorOf(Props[B], "Bactor")
b ! 12
}
}
}
class B extends Actor {
val sender_ = sender()
override def receive: Receive = {
case _: Int => {
println(sender_)
println("inside B receive block")
sender_ ! 22
}
}
}
我期待println(sender_)
能够打印演员A
信息,但它正在打印死信。正如演员A
所说,我不明白为什么它会打印死信。如何修改这个以便我可以获得发件人的参考?
答案 0 :(得分:0)
在receive
方法中捕获发件人:
class B extends Actor {
override def receive: Receive = {
case _: Int =>
val sender_ = sender()
println(sender_)
println("inside B receive block")
sender_ ! 22
}
}
请注意,处于当前状态的角色A
无法处理Int
(这是B
向A
发送的内容)。