Trey Collier
Trey Collier

Reputation: 129

Scala Akka OneForOneStrategy java.lang.NullPointerException: null

I am trying to implement fault tolerance within my actor system for my Scala project, to identify errors and handle them. I am using Classic actors. Each supervisor actor has 5 child actors, if one of these child actors fails, I want to restart that actor, and log the error, and as I mentioned, handle the problem that caused this actor to fail.

I implemented a One-For-One SupervisorStrategy in my Supervisor actor class as such:

override val supervisorStrategy =
    OneForOneStrategy(maxNrOfRetries = 5, withinTimeRange = 1.minute) {
      case e: ArithmeticException =>
        logger.error(s"Supervisor: $e from $sender; Restarting!")
        Restart
      case e: NullPointerException =>
        logger.error(s"Supervisor: $e from $sender; Restarting!")
        Restart
      case e: IllegalArgumentException =>
        logger.error(s"Supervisor: $e from $sender; Restarting!")
        Restart
      case _: Exception =>
        logger.error(s"Supervisor: Unknown exception from $sender; Escalating!")
        Restart 
    }

I have also tried something as simple as:

override val supervisorStrategy =
    OneForOneStrategy() {
      case _ =>
        logger.error(s"Supervisor: Unknown exception from $sender; Escalating!")
        Restart 
    }

I added the following code into one of the supervisor methods that tells the actors to start working:

if(!errorSent){
   errorSent = true
   actor ! new NullPointerException
}

With a var errorSent being declared in my supervisor, and the actor throwing the exception when an exception message is received, it is implemented with this var so that it only occurs once as to not put the actor in a state of infinitely restarting. I did this because the system never/rarely fails. But, in the log file, I do not get what I expect, but the following:

13:41:19.893 [ClusterSystem-akka.actor.default-dispatcher-21] ERROR akka.actor.OneForOneStrategy - null
java.lang.NullPointerException: null

I have looked at so many examples, as well as the Akka documentation for fault tolerance for both types and classic actors, but I cannot get this to work.

Upvotes: 0

Views: 981

Answers (0)

Related Questions