Search code examples
scalaakkaakka-supervisionakka-actor

Akka custom supervisor not taking effect


In 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"
}

which refers to the following:

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}
    }
  }
}

My Actor is instantiated like so

object Singleton {
  val actorSystem = ActorSystem("main")
  val logger: ActorRef = actorSystem.actorOf(Props(new Logger()))
}

and

val miner: ActorRef = 
    Singleton.actorSystem.actorOf(Props(new Miner()))

However, when miner hits an Exception, it still restarts (it is still using the default supervisor strategy)


As a side note, my actors have very simple internal state. All failures are due to external resources (ie. Futures not returned because the server sent a bad response) so the default strategy of restarting the actor is not necessary.


Solution

  • I think it should be

    akka.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"
    }