谁能用ActorIdentity一个很好的例子来解释如何以及何时使用?
从文档中我可以发现“有一个内置的 Identity 消息,所有 Actor 都会理解该消息,并使用包含 ActorRef 的 ActorIdentity 消息自动回复”。
该声明是否意味着获得的演员说actorSelector我的演员中包含了 ActorIdentity 消息?
ActorSelection actorSelector = getContext().actorSelection("/A/B/*");
Run Code Online (Sandbox Code Playgroud)
当您Identify向ActorSelection参与者发送消息时,如果存在,参与者将回复一条ActorIdentity消息。
如果演员存在,ActorIdentity消息将包含一个Some(actorRef). ActorRef向s发送消息比向s发送消息效率更高ActorSelection。
例如(来自手册):
class Follower extends Actor {
val identifyId = 1
context.actorSelection("/user/another") ! Identify(identifyId)
def receive = {
case ActorIdentity(`identifyId`, Some(ref)) =>
context.watch(ref)
context.become(active(ref))
case ActorIdentity(`identifyId`, None) => context.stop(self)
}
def active(another: ActorRef): Actor.Receive = {
case Terminated(`another`) => context.stop(self)
}
}
Run Code Online (Sandbox Code Playgroud)
手册中涉及此内容的部分称为通过演员选择识别演员。