Last active
February 6, 2016 04:36
-
-
Save matsu-chara/b36d5842fb8f7e1da19c to your computer and use it in GitHub Desktop.
primary constructorの中身はRestartなら二回呼ばれる
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.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