Skip to content

Instantly share code, notes, and snippets.

@wlk
Last active November 19, 2017 13:31
Show Gist options
  • Save wlk/7b6f76b748de7a2ea5ff0de8b7bb8989 to your computer and use it in GitHub Desktop.
Save wlk/7b6f76b748de7a2ea5ff0de8b7bb8989 to your computer and use it in GitHub Desktop.
import akka.actor.SupervisorStrategy.Restart
import akka.actor.{Actor, ActorInitializationException, ActorSystem, OneForOneStrategy, Props}
object Main extends App {
var attempt: Int = 0
class FailingActor extends Actor {
if (attempt == 0) {
// throw exception only for the first time, simulating first attempt to create an actor that fails
attempt = 1
throw new RuntimeException("failed to create an instance")
}
override def receive: Receive = {
case _ =>
}
override def preStart(): Unit = {
// Same behavior when error happens in 'preRestart'
// throw new RuntimeException("failed to create an instance")
}
}
class TestSuperVisor extends Actor {
context.actorOf(Props(classOf[FailingActor]))
override def receive: Receive = {
case _ =>
}
override val supervisorStrategy = OneForOneStrategy(loggingEnabled = false) {
case _: ActorInitializationException =>
Restart
}
}
private implicit lazy val system: ActorSystem = ActorSystem("test")
system.actorOf(Props(classOf[TestSuperVisor]))
Thread.sleep(1000)
system.terminate()
/*
This is unexpected error that gets logged:
[ERROR] [11/19/2017 13:52:02.095] [test-akka.actor.default-dispatcher-3] [akka://test/user/$a/$a] a.a.ActorCell changing Recreate into Create after akka.actor.ActorInitializationException: akka://test/user/$a/$a: exception during creation
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment