Guest User

Untitled

a guest
Nov 20th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. import akka.actor.SupervisorStrategy.Restart
  2. import akka.actor.{Actor, ActorInitializationException, ActorSystem, OneForOneStrategy, Props}
  3.  
  4. object Main extends App {
  5. var attempt: Int = 0
  6.  
  7. class FailingActor extends Actor {
  8.  
  9. if (attempt == 0) {
  10. // throw exception only for the first time, simulating first attempt to create an actor that fails
  11. attempt = 1
  12. throw new RuntimeException("failed to create an instance")
  13. }
  14.  
  15. override def receive: Receive = {
  16. case _ =>
  17. }
  18.  
  19. override def preStart(): Unit = {
  20. // Same behavior when error happens in 'preRestart'
  21. // throw new RuntimeException("failed to create an instance")
  22. }
  23.  
  24. }
  25.  
  26. class TestSuperVisor extends Actor {
  27.  
  28. context.actorOf(Props(classOf[FailingActor]))
  29.  
  30. override def receive: Receive = {
  31. case _ =>
  32. }
  33.  
  34. override val supervisorStrategy = OneForOneStrategy(loggingEnabled = false) {
  35. case _: ActorInitializationException =>
  36. Restart
  37. }
  38. }
  39.  
  40. private implicit lazy val system: ActorSystem = ActorSystem("test")
  41.  
  42. system.actorOf(Props(classOf[TestSuperVisor]))
  43.  
  44. Thread.sleep(1000)
  45.  
  46. system.terminate()
  47.  
  48. /*
  49. This is unexpected error that gets logged:
  50. [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
  51. */
  52.  
  53. }
Add Comment
Please, Sign In to add comment