5

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/*");

1 Answer 1

5

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 ActorRefs than ActorSelections.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.