I am learning akka-remote and one of the things I do in my LocalActorSystem
is getting remote actor reference and send messages to him
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
}
}
}
My 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 watch remoteActor
so that if its dead, LocalActorSystem get to know. So I did
val remote = context.actorSelection("akka.tcp://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor")
context watch remote
but then compiler fails with following message
Question
- How come I am able to send message to
ActorSelection
since it is notActor
? - How can I watch 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