Skip to content

Instantly share code, notes, and snippets.

@matsu-chara
Last active July 4, 2016 17:17
Show Gist options
  • Select an option

  • Save matsu-chara/c4f0252bc09f93810786ee6576723d60 to your computer and use it in GitHub Desktop.

Select an option

Save matsu-chara/c4f0252bc09f93810786ee6576723d60 to your computer and use it in GitHub Desktop.
再起動時にpreStartでなげるよりもMailBoxに積まれていたもののほうが早く実行される
package ex2
import akka.actor.SupervisorStrategy._
import akka.actor._
import scala.concurrent.Await
import scala.concurrent.duration.Duration
object Main1 extends App {
val system = ActorSystem()
try {
val actor = system.actorOf(Props[SupervisorExceptionActor])
actor ! 1
actor ! 2
Thread.sleep(1000)
(3 to 10).foreach {actor ! _}
} finally {
Thread.sleep(1000)
Await.result(system.terminate(), Duration.Inf)
}
}
class SupervisorExceptionActor extends Actor {
val actor = context.actorOf(Props[InitializationException])
override def supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 2) {
case _: ActorInitializationException ⇒ Restart
case _: ActorKilledException ⇒ Stop
case _: DeathPactException ⇒ Stop
case _: Exception ⇒ Restart
}
def receive = {
case Terminated(ref: ActorRef) => println(ref)
case x => actor forward x
}
}
class InitializationException extends Actor {
override def preStart() = {
super.preStart()
self ! "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
println("starting")
}
override def postStop() = {
super.postStop()
println("postStop")
}
override def preRestart(reason: Throwable, message: Option[Any]): Unit = {
super.preRestart(reason, message)
println("preRestart")
}
override def postRestart(reason: Throwable): Unit = {
super.postRestart(reason)
println("postRestart")
}
def receive = {
case 1 =>
println(1)
Thread.sleep(1000)
case 2 =>
println(2)
throw new RuntimeException("a")
case x =>
println(x)
}
}
[info] Running ex2.Main1
starting
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
1
2
[ERROR] [07/05/2016 02:16:13.870] [default-akka.actor.default-dispatcher-4] [akka://default/user/$a/$a] a
java.lang.RuntimeException: a
at ex2.InitializationException$$anonfun$receive$2.applyOrElse(InitializationException.scala:67)
at akka.actor.Actor$class.aroundReceive(Actor.scala:480)
at ex2.InitializationException.aroundReceive(InitializationException.scala:39)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526)
at akka.actor.ActorCell.invoke(ActorCell.scala:495)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257)
at akka.dispatch.Mailbox.run(Mailbox.scala:224)
at akka.dispatch.Mailbox.exec(Mailbox.scala:234)
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
postStop
preRestart
starting
postRestart
3
4
5
6
7
8
9
10
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
postStop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment