Monitoring an actor life cycle using DeathWatch
In some scenarios, it becomes necessary to check continuously whether a particular service is running or not. In this recipe, we will learn how to monitor an actor.
To step through this recipe, you need to import the Hello-Akka
project.
How to do it...
- Let's create a file,
DeathWatch.scala
, in packagecom.packt.chapter2
. - Add the following imports to the top of the file:
import akka.actor.{Actor, ActorSystem, Props, Terminated}
- Create the following case objects as messages:
case object Service case object Kill
- Define a service actor, which would be monitored.
class ServiceActor extends Actor { def receive = { case Service => println("I provide a special service") } }
- Create an actor, which would be creating
ServiceActor
as its child.
class DeathWatchActor extends Actor { val child = context.actorOf(Props[ServiceActor], "serviceActor...