scala - Akka actorSelection 失败后重新连接

标签 scala akka actor

我有一个 Actor ,它监视另一个 Actor (这是远程的,因此不能直接引用)。它通过使用 actorSelection 获得对 actor 的引用。和 Identify然后观察结果 ActorRef .那个有效。

但是现在我想在另一个参与者终止时自动重新连接,所以我重用了相同的 actorSelection (Actor get 在同一位置实例化),但这次通过 Identify 进行查找。永远失败。我不知道为什么它会起作用,当 Actor 已经在开始时被实例化而不是,否则。

编辑:奇怪的是,在第一次连接之前存在关联错误,而在尝试重新连接时却没有,即使远程 jvm 已完全终止。我刚刚注意到,如果您在失败后等待大约一分钟或更长时间,关联错误将返回并且连接再次成功。有没有办法配置这个(它是一个缓存?)机制。

这是标准行为还是我做错了什么?

万一我搞砸了我的代码:

object ServerMonitor {
  case object Request
  case class Reply(ref: ActorRef)
}

class ServerMonitor(path: String) extends Actor with ActorLogging {
  import ServerMonitor._

  var server: Option[ActorRef] = None  
  var listeners: Set[ActorRef] = Set.empty

  def receive = {
    case ActorIdentity("server", Some(ref)) =>
      server = Some(ref)
      context.watch(ref)
      listeners.foreach(_ ! Reply(ref))
      listeners = Set.empty
      log.info(s"connected to the server at $path")

    case ActorIdentity("server", None) =>
      server = None
      log.warning(s"couldnt reach the server at $path")
      import context.dispatcher
      context.system.scheduler.scheduleOnce(1 second) {
        context.actorSelection(path) ! Identify("server")
      }

    case Terminated(ref) =>
      log.warning("server terminated")
      server = None
      context.actorSelection(path) ! Identify("server")

    case Request =>
      server.fold { 
        listeners += sender
      } { ref =>
        sender ! Reply(ref)
      }
  }

  override def preStart() {
    context.actorSelection(path) ! Identify("server")
  }
}

最佳答案

好的,我刚刚发现,问题是什么。有一个配置值:

# The length of time to gate an address whose name lookup has failed
# or has explicitly signalled that it will not accept connections
# (remote system is shutting down or the requesting system is quarantined).
# No connection attempts will be made to an address while it remains
# gated. Any messages sent to a gated address will be directed to dead
# letters instead. Name lookups are costly, and the time to recovery
# is typically large, therefore this setting should be a value in the
# order of seconds or minutes.
gate-invalid-addresses-for = 60 s

可以设置为低,以允许在远程系统恢复后快速重新连接。即使考虑到上述原因,60 年代对我来说似乎也高得离谱。

关于scala - Akka actorSelection 失败后重新连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19251830/

相关文章:

Scala 随机数生成器不生成范围之间的唯一随机数

scala - IntelliJ 似乎没有选择某些 sbt 库,没有代码完成

java - Akka - 如何重新启动路由器生成的子进程?

c# - Serilog 不适用于 Reliable Actor Services

scala - 当客户端 actor 与远程 actor 断开连接时如何禁用 Akka 错误消息?

scala - Actor (scala/akka) : is it implied that the receive method will be accessed in a threadsafe manner?

scala - 在 scala 中,如何以正确的顺序从映射中获取键和值的数组(第 i 个键用于第 i 个值)?

scala - 在 Scala 中创建一个懒惰的 var

java - 如何在 Java 中初始化 FSM actor?

multithreading - 将 Akka Actor 固定/运行到主线程