6

我正在尝试监督 Akka Actor,更具体地说使用ClusterSingletonManager. 我正在尝试对异常、日志和 Actor 的生命周期进行更多控制。

不幸的是,在实施解决方案后,我让 Singleton Actor 抛出异常,但日志中没有显示任何内容,Actor 或 Cluster 也没有关闭。

我的实现如下:

object SingletonSupervisor {
  case class CreateSingleton(p: Props, name: String)
}

class SingletonSupervisor extends Actor with ActorLogging {
  override val supervisorStrategy =
    OneForOneStrategy(maxNrOfRetries = 0, withinTimeRange = 1.minute) {
      case x: ActorInitializationException =>
        log.error(s"Actor=<${x.getActor}> trowed an exception=<${x.getCause}> with message=<${x.getMessage}>")
        Stop

      case x: ActorKilledException => Stop

      case x: DeathPactException => Stop

      case x: Exception =>
        log.error(s"Some actor threw an exception=<${x.getCause}> with message=<${x.getMessage}>, trace=<${x.getStackTrace}>")
        Escalate
    }

  def receive = {
    case CreateSingleton(p: Props, name: String) =>
      sender() ! context.actorOf(p)
      context.actorOf(ClusterSingletonManager.props(
        singletonProps = p,
        terminationMessage = PoisonPill,
        settings = ClusterSingletonManagerSettings(context.system)),
        name = name)
  }
}

那么,是否有可能监督 Cluster Singlegon?如果可能的话,我应该如何解决这个问题?

4

1 回答 1

6

一种可能的解决方案是创建主管角色,它使用给定的子节点生成给定的子节点supervisorStrategy并将消息转发给它的子节点。

这是主管演员:

class SupervisorActor(childProps: Props, override val supervisorStrategy) extends Actor {

  val child = context.actorOf(childProps, "supervised-child")

  def receive: Receive = {
    case msg => child forward msg
  }
}

以下是您如何将受监督的参与者创建为集群单例

context.actorOf(ClusterSingletonManager.props(
        singletonProps = Props(classOf[SupervisorActor], p, supervisorStrategy),
        terminationMessage = PoisonPill,
        settings = ClusterSingletonManagerSettings(context.system)),
        name = name)
于 2016-04-19T11:12:39.360 回答