TheAkkaWay icon indicating copy to clipboard operation
TheAkkaWay copied to clipboard

tell(!)方法的sender与陷阱

Open PipsqueakH opened this issue 8 years ago • 1 comments

actor ! msg

中,tell 方法的签名是

  def !(message: Any)(implicit sender: ActorRef = Actor.noSender): Unit
  final def tell(msg: Any, sender: ActorRef): Unit = this.!(msg)(sender)

这里的Actor.noSender的定义为

  /**
   * Default placeholder (null) used for "!" to indicate that there is no sender of the message,
   * that will be translated to the receiving system's deadLetters.
   */
  final val noSender: ActorRef = null

如果在一个actor内部调用tell方法,noSender即为自己,如果在外部调用的话即为null.

另,在测试时, 可以显示指定消息的发送者为TestProbe,例如

actor.tell(msg, TestProbe().ref)

这在测试需要接受来自多个不同的actor的消息的actor时非常有用。

PipsqueakH avatar May 12 '17 14:05 PipsqueakH

@PipsqueakH 这不算什么陷阱吧,这是基本特性啊。

gabry-lab avatar Aug 13 '18 07:08 gabry-lab