gpt4 book ai didi

scala - 如何观看远程 Akka Actor?

转载 作者:行者123 更新时间:2023-12-04 23:44:52 25 4
gpt4 key购买 nike

我正在学习 akka-remote 和我在 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
}
}
}

我的 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"
}
}

我也想看 remoteActor这样如果它死了,LocalActorSystem 就会知道。所以我做了
  val remote = context.actorSelection("akka.tcp://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor")
context watch remote

但随后编译器失败并显示以下消息

enter image description here

问题
  • 我怎么能发消息到ActorSelection因为它不是 Actor ?
  • 如何观看 RemoteActor?

  • 更新
    但是,已弃用的 API 不会提示
    val remote = context.actorFor("akka.tcp://HelloRemoteSystem@127.0.0.1:5150/user/RemoteActor")
    context watch remote

    最佳答案

    当您通过 actorSelection 进行查找时,您返回的对象类型是 ActorSelection而不是 ActorRef .现在,一个 ActorSelection确实支持tell (!)ask (?)因此您可以像使用 ActorRef 一样与之交互.但是通过 actorSelection 查找 Actor 支持通配符的概念,所以 ActorSelection你回来可能代表不止一个 Actor ,并允许你向多个 Actor 发送消息。例如,如果您这样做:
    system.actorSelection("/user/foo/*")
    这会给你一个ActorSelection对于 parent 下的所有 child ActorRef绑定(bind)到名称 foo .如果有两个 child ,您通过 ActorSelection 发送消息,该消息将传递给两个 child 。

    在您的情况下,看起来您正在查找单个 Actor 实例。在这种情况下,您可以获得 ActorRef来自您的ActorSelection调用 resolveOne在上面。这将返回 Future[ActorRef]完成后将为您提供 ActorRef你可以远程观看。您也可以发送 ActorSelection一个 Identify消息并等待ActorIdentity包含要观看的引用的响应。

    您应该查看文档 here ,特别是 Identifying Actors via Actor Selection部分。

    关于scala - 如何观看远程 Akka Actor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31015347/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com