Overriding the life cycle hooks of an actor
Since we are talking about supervision and monitoring of actors, you should understand the life cycle hooks of an actor. In this recipe, you will learn how to override the life cycle hooks of an actor when it starts, stops, prestarts, and postrestarts.
How to do it...
- Create a file called
ActorLifeCycle.scala
in packagecom.packt.chapter2
. - Add the following imports to the top of the file:
import akka.actor._ import akka.actor.SupervisorStrategy._ import akka.util.Timeout import scala.concurrent.Await import scala.concurrent.duration._ import akka.pattern.ask
- Create the following messages to be sent to the actors:
case object Error case class StopActor(actorRef: ActorRef)
- Create an actor as follows, and override the life cycle methods:
class LifeCycleActor extends Actor { var sum = 1 override def preRestart(reason: Throwable, message: Option...