[英]How to watch remote Akka Actor?
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 我正在学习akka-remote,我在
LocalActorSystem
做的一LocalActorSystem
是获取远程actor参考并向他发送消息
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 我的
Remote
看起来像
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. 我还想看一下
remoteActor
这样如果死了,LocalActorSystem就知道了。 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 题
ActorSelection
since it is not Actor
? ActorSelection
发送消息,因为它不是Actor
? Update 更新
However, the deprecated API does not complain 但是,弃用的API不会抱怨
val remote = context.actorFor("akka.tcp://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor")
context watch remote
When you do a lookup via actorSelection
, that type of object you get back is an ActorSelection
and not an ActorRef
. 当您通过
actorSelection
进行查找时,您获得的那种类型的对象是ActorSelection
而不是ActorRef
。 Now, an ActorSelection
does support both tell (!)
and ask (?)
so you can interact with it the same way you would an ActorRef
. 现在,
ActorSelection
确实支持tell (!)
和ask (?)
因此您可以像使用ActorRef
一样与它进行ActorRef
。 But an looking up actors via actorSelection
supports the concept of a wild card, so the ActorSelection
you get back potentially represents more than one actor and would allow you to send messages to more than one actor. 但是通过
actorSelection
查找的actor支持通配符的概念,因此您返回的ActorSelection
可能代表多个actor,并允许您向多个actor发送消息。 For instance, if you did : 例如,如果您这样做:
system.actorSelection("/user/foo/*")
this would give you an ActorSelection
for all children under the parent ActorRef
bound to the name foo
. 这将给你一个
ActorSelection
父下的所有子ActorRef
绑定到名称foo
。 If there were two children and you send a message via that ActorSelection
, that message would be delivered to both children. 如果有两个孩子并且您通过该
ActorSelection
发送消息,则该消息将被传递给两个孩子。
In your case, it looks like you are looking up a single actor instance. 在您的情况下,看起来您正在查找单个actor实例。 In that case, you can get an
ActorRef
from your ActorSelection
by calling resolveOne
on it. 在这种情况下,您可以通过调用
resolveOne
从ActorSelection
获取ActorRef
。 This will return a Future[ActorRef]
that when completed will provide you with an ActorRef
that you can watch remotely. 这将返回
Future[ActorRef]
,完成后将为您提供可以远程观看的ActorRef
。 You could also send the ActorSelection
an Identify
message and wait for the ActorIdentity
response containing the ref to watch. 您还可以向
ActorSelection
发送Identify
消息,并等待包含要观看的ref的ActorIdentity
响应。
You should check out the docs here , specifically the Identifying Actors via Actor Selection
section. 您应该在这里查看文档,特别是
Identifying Actors via Actor Selection
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.