Akka发件人参考包含死信

时间:2018-01-17 15:43:28

标签: scala akka

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所说,我不明白为什么它会打印死信。如何修改这个以便我可以获得发件人的参考?

1 个答案:

答案 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(这是BA发送的内容)。