Last active
March 10, 2017 23:59
-
-
Save j5ik2o/c2f7e2d2bb2e7a1d5e2477230df64c29 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val childProps = Props(classOf[EchoActor]) | |
| val stopBackOffOptions = Backoff.onStop(childProps, "c1", 100.millis, 3.seconds, 0.2) | |
| val failureBackOffOptions = Backoff.onFailure(childProps, "c1", 100.millis, 3.seconds, 0.2) | |
| val stopBackOffSupervisor = system.actorOf(BackoffSupervisor.props(stopBackOffOptions)) | |
| val failureBackOffSupervisor = system.actorOf(BackoffSupervisor.props(failureBackOffOptions)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class EventListener extends Actor with ActorLogging { | |
| override def receive: Receive = { | |
| case ChildStarted(Some(ChildException(msg))) => | |
| log.info(s"child actor was started: $ex") | |
| sender() ! msg // retry to send a message | |
| case ChildStopped => | |
| log.info("child actor was stopped") | |
| } | |
| } | |
| val childProps = Props(classOf[EchoActor]) | |
| val stopBackOffOptions = Backoff.onStop(childProps, "c1", 100.millis, 3.seconds, 0.2) | |
| .withEventSubscriber(Some(eventListener)) | |
| val stopBackOffSupervisor = system.actorOf(BackoffSupervisor.props(stopBackOffOptions)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val childProps = Props(classOf[EchoActor]) | |
| val stopBackOffOptions = Backoff.onStop(childProps, "c1", 100.millis, 3.seconds, 0.2) | |
| .withOnStartChildHandler { case (supervisorRef, exOpt) => | |
| system.log.info(s"on start child: $supervisorRef, $exOpt") | |
| exOpt.foreach(supervisorRef ! _) // retry to send a message | |
| }.withOnStopChildHandler { supervisorRef => | |
| system.log.info(s"on stop child: $supervisorRef") | |
| } | |
| val stopBackOffSupervisor = system.actorOf(BackoffSupervisor.props(stopBackOffOptions)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| case class ChildException(errorMessage: Any) | |
| extends Exception() | |
| object ChildActor { | |
| def props = Props[ChildActor] | |
| } | |
| class ChildActor extends Actor { | |
| override def receive: Receive = { | |
| case msg => | |
| throw ChildException(msg) | |
| } | |
| } | |
| object Supervisor { | |
| def props(backoffOptions: BackoffOptions): Props = | |
| Props(new Supervisor(backoffOptions)) | |
| } | |
| class Supervisor(backoffOptions: BackoffOptions) | |
| extends BackoffOnRestartSupervisor { | |
| override val minBackoff = backoffOptions.minBackoff | |
| override val maxBackoff = backoffOptions.maxBackoff | |
| override val randomFactor = backoffOptions.randomFactor | |
| override val reset = backoffOptions | |
| .reset.getOrElse(AutoReset(minBackoff)) | |
| override def childProps: Props = ChildActor.props | |
| override def childName: String = "child" | |
| override def handleCommand: Receive = { | |
| case msg => | |
| child.get forward msg | |
| } | |
| override def onStartChild(exOpt: Option[Throwable]): Unit = | |
| exOpt.foreach { case ChildException(msg) => | |
| child.get ! msg // retry to send a message | |
| } | |
| } | |
| val backOffOptions = Backoff.custom(childProps, "c1", 100.millis, 3.seconds, 0.2) | |
| val backOffSupervisor = system.actorOf(Supervisor.props(backOffOptions)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment