Skip to content

Instantly share code, notes, and snippets.

@dbalduini
Last active August 29, 2015 14:00
Show Gist options
  • Save dbalduini/7af50dd4629bc3b7f2b5 to your computer and use it in GitHub Desktop.
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
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