在 src/main/resources/application.conf
actor {
# The guardian "/user" will use this class to obtain its supervisorStrategy.
# It needs to be a subclass of akka.actor.SupervisorStrategyConfigurator.
# In addition to the default there is akka.actor.StoppingSupervisorStrategy.
guardian-supervisor-strategy = "config.ResilientSupervisorStrategy"
}
这是指以下内容:
package config
import akka.actor._
import akka.actor.SupervisorStrategy._
final class ResilientSupervisorStrategy extends SupervisorStrategyConfigurator {
override def create(): SupervisorStrategy = {
OneForOneStrategy(){
case _: ActorInitializationException ⇒ Stop
case _: ActorKilledException ⇒ Stop
case _: DeathPactException ⇒ Stop
case _: Exception ⇒ Resume}
}
}
}
我的演员是这样实例化的
object Singleton {
val actorSystem = ActorSystem("main")
val logger: ActorRef = actorSystem.actorOf(Props(new Logger()))
}
和
val miner: ActorRef =
Singleton.actorSystem.actorOf(Props(new Miner()))
但是,当miner
遇到异常时,它仍然会重新启动(它仍然使用默认的主管策略)
作为旁注,我的演员有非常简单的内部状态。所有失败都是由于外部资源造成的(即,由于服务器发送了错误的响应,Futures 未返回),因此不需要重启 actor 的默认策略。