Applying the supervision strategy

While overriding the default supervisorStrategy, all we do is define the value with arguments and provide a Decider; this decider contains the logic to be implemented in case of exceptions. It looks like this:

import akka.actor.SupervisorStrategy.{Resume, Restart} 
 
override val supervisorStrategy = 
  OneForOneStrategy( 
    maxNrOfRetries = 3, 
    withinTimeRange = 1 minute 
  ){ 
    case _: ArithmeticException => { 
      log.info("Supervisor handling ArithmeticException! n Resuming!") 
      Resume 
    } 
    case _: Exception => { 
      log.info("Supervisor handling Exception! n Restarting!") 
      Restart 
    } 
  } 

Here, we have defined a OneForOneStrategy, and on a case by case basis, the action to be performed in regards to the failing actor. A full example ...

Get Learning Scala Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.