How to watch deleted Akka Actor?

I am researching akka-remote and one of the things I do in mine LocalActorSystem

is get a link to the remote actor and send messages to it

class LocalActor extends Actor {
  val remote = context.actorSelection("akka.tcp://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor")
  var counter = 0

  def receive = {
    case "START" =>
      remote ! "Hello from the LocalActor"
    case msg: String =>
      println(s"LocalActor received message: '$msg'")
      if (counter < 5) {
        sender ! "Hello back to you"
        counter += 1
      }
  }
}

      

Mine Remote

looks like

object Remote extends App {
  val system = ActorSystem("HelloRemoteSystem", ConfigFactory.load("remote"))
  val remoteActor = system.actorOf(Props[RemoteActor], name = "RemoteActor")
  remoteActor ! "The RemoteActor is alive"
}

class RemoteActor extends Actor {
  def receive = {
    case msg: String =>
      println(s"RemoteActor received message '$msg'")
      sender ! "Hello from the RemoteActor"
  }
}

      

I also wanted to see remoteActor

its dead, LocalActorSystem find out. So I did

  val remote = context.actorSelection("akka.tcp://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor")
  context watch remote

      

but the compiler did not execute the following message

enter image description here

Question

  • Why can I post a message to ActorSelection

    , since it's not Actor

    ?
  • How can I see RemoteActor?

Update
However, the deprecated API does not complain

val remote = context.actorFor("akka.tcp://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor")
  context watch remote

      

+3


source to share


1 answer


When you search through actorSelection

, this return type will be actorSelection

, not ActorRef

. Now actorSelection

supports both tell (!)

so and ask (?)

so you can interact with it the same way you do ActorRef

. But searching for actors with actorSelection

supports the concept of a wild card, so actorSelection

what you get potentially represents more than one actor and allows you to send messages to multiple actors. For example, if you did this:

system.actorSelection("/user/foo/*")

this will give you actorSelection

for all children under the parent ActorRef

associated name foo

. If there were two children and you sent a message through this message actorSelection

, this message will be delivered to both children.



In your case, it looks like you are viewing an instance of one actor. In this case, you can get ActorRef

out actorSelection

by calling resolveOne

on it. This will return Future[ActorRef]

, which upon completion will provide you with ActorRef

one that you can watch remotely. You can also send message actorSelection

a Identify

and wait for a response ActorIdentity

containing ref to view.

This is where you should check the docs, in particular the section Identifying Actors via Actor Selection

.

+6


source







All Articles