Skip to content

Instantly share code, notes, and snippets.

@jamie-allen
Created April 12, 2013 19:02
Show Gist options
  • Save jamie-allen/5374304 to your computer and use it in GitHub Desktop.
Save jamie-allen/5374304 to your computer and use it in GitHub Desktop.
import akka.actor.OneForOneStrategy
import akka.actor.SupervisorStrategy._
import akka.actor.ActorSystem
import akka.actor.Actor
import akka.util.duration._
import akka.actor.Props
case object Start
case object MakeCrash
// Create a supervisor and an actor that dies 5 times in 1 minute, check output in logs
class MySupervisor extends Actor {
override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 5, withinTimeRange = 1 minute) {
case ae: ArithmeticException => Restart
}
def child = context.actorOf(Props[MyChild])
def receive = {
case Start => child ! MakeCrash
}
}
class MyChild extends Actor {
def receive = {
case MakeCrash => 1 / 0
}
}
object TestBootstrap extends App {
val system = ActorSystem()
val supervisor = system.actorOf(Props[MySupervisor])
supervisor ! Start
Thread.sleep(2000)
supervisor ! Start
Thread.sleep(2000)
supervisor ! Start
Thread.sleep(2000)
supervisor ! Start
Thread.sleep(2000)
supervisor ! Start
Thread.sleep(2000)
// Should be dead now
supervisor ! Start
Thread.sleep(2000)
system.shutdown
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment