Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- */
- }
Add Comment
Please, Sign In to add comment