Skip to content

Instantly share code, notes, and snippets.

@matsu-chara
Last active February 6, 2016 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsu-chara/b36d5842fb8f7e1da19c to your computer and use it in GitHub Desktop.
Save matsu-chara/b36d5842fb8f7e1da19c to your computer and use it in GitHub Desktop.
primary constructorの中身はRestartなら二回呼ばれる
import akka.actor.SupervisorStrategy.{Resume, Restart, Stop}
import akka.actor._
object Main extends App {
val system = ActorSystem()
try {
val actor = system.actorOf(Props[SupervisorExceptionActor])
actor ! 1
actor ! 2
actor ! 3
Thread.sleep(5000)
} finally {
system.shutdown()
}
}
class SupervisorExceptionActor extends Actor {
val actor = context.actorOf(Props[KilledActor])
override def supervisorStrategy = OneForOneStrategy() {
case _: ActorInitializationException ⇒ Stop
case _: ActorKilledException ⇒ Stop
case _: DeathPactException ⇒ Stop
case _: Exception ⇒ Restart
}
def receive = {
case Terminated(ref: ActorRef) => println(ref)
case x => actor forward x
}
}
class KilledActor extends Actor {
val a = {
println("initializing a")
1
}
override def preStart() = {
super.preStart()
println("starting")
}
override def postStop() = {
super.postStop()
println("stoped")
}
override def preRestart(reason: Throwable, message: Option[Any]): Unit = {
super.preRestart(reason, message)
println("preRestart")
}
def receive = {
case 2 => throw new RuntimeException("death")
case _ => println(a)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment