In this recipe, we will learn about AllForOneStrategy
for child actors.
AllForOneStrategy
implies that in case of failure of any one actor under a supervisor, the strategy will be applied to all the children under supervision.
To step through this recipe, we need to import Hello-Akka
project in IDE.
- Let's create a scala file,
AllForOneStrategy.scala
, in packagecom.packt.chapter2
. - Add the following imports to the top of the file:
import akka.actor.SupervisorStrategy.{Escalate, Stop, Resume, Restart} import akka.actor._ import scala.concurrent.duration._
- Create messages as case classes.
case class Add(a: Int, b: Int) case class Sub(a: Int, b: Int) case class Div(a: Int, b: Int)
- Let's create a calculator actor as follows:
class Calculator(printer: ActorRef) extends Actor { override def preRestart(reason: Throwable, message: Option[Any]): Unit = { ...