Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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