Skip to content

Instantly share code, notes, and snippets.

@matsu-chara
Created February 2, 2016 13:43
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/72b27db80c6213d574a2 to your computer and use it in GitHub Desktop.
Save matsu-chara/72b27db80c6213d574a2 to your computer and use it in GitHub Desktop.
1の処理が終わらないとkillされない予感
import akka.actor.SupervisorStrategy.{Restart, Stop}
import akka.actor._
object Main extends App {
val system = ActorSystem()
try {
val actor = system.actorOf(Props[SupervisorExceptionActor])
actor ! 1
actor ! 2
} finally {
Thread.sleep(5000)
system.shutdown()
}
}
class SupervisorExceptionActor extends Actor {
val actor = context.actorOf(Props[KilledActor])
context.watch(actor)
override def supervisorStrategy = OneForOneStrategy()({
case _: ActorInitializationException => Stop
case _: ActorKilledException => {println("killed"); Stop}
case _: DeathPactException => Stop
case _: Exception => Restart
})
def receive = {
case 1 => actor ! 1
case 2 => {
println("kill")
actor ! Kill
}
}
}
class KilledActor extends Actor {
def receive = {
case 1 => {
println("1")
// ↓をfalseにするとkilledが出力される
while(true) {
Thread.sleep(500)
}
}
Thread.sleep(1000)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment