Last active
August 29, 2015 14:00
-
-
Save dbalduini/7af50dd4629bc3b7f2b5 to your computer and use it in GitHub Desktop.
Akka Actor that tracks actors for shutting down the system when they all are terminated. @see http://letitcrash.com/post/30165507578/shutdown-patterns-in-akka-2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.{ActorLogging, Terminated, ActorRef, Actor} | |
import scala.collection.mutable | |
object Reaper { | |
case class WatchMe(ref: ActorRef) | |
} | |
class Reaper extends Actor with ActorLogging { | |
import Reaper._ | |
val souls = new mutable.ListBuffer[ActorRef]() | |
def receive = { | |
case WatchMe(ref) => | |
context.watch(ref) | |
souls += ref | |
case Terminated(ref) => | |
souls -= ref | |
if (souls.isEmpty) allSoulsReaped() | |
} | |
def allSoulsReaped() { | |
log.warning("All Souls Reaped! System shutting down...") | |
context.system.shutdown() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment