Reputation: 1568
Can anyone explain how and when to use ActorIdentity
with a good example?
From documents I can find that "There is a built-in Identify message that all Actors will understand and automatically reply to with a ActorIdentity message containing the ActorRef".
Does that statement mean obtained actor say actorSelector
have ActorIdentity message wrapped in my actor?
ActorSelection actorSelector = getContext().actorSelection("/A/B/*");
Upvotes: 5
Views: 740
Reputation: 24040
When you send an Identify
message to an ActorSelection
the actor will respond, if it exists, with an ActorIdentity
message.
If the actor exists the ActorIdentity
message will contain a Some(actorRef)
. It is more efficient to send messages to ActorRef
s than ActorSelection
s.
For example (from the manual):
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)
}
}
The section in the manual that covers this is called Identifying Actors via Actor Selection.
Upvotes: 5