```scala object InnerChild { case class TestMessage(msg: String) class ChildException extends Exception def props = Props[InnerChild] }
class InnerChild extends Actor with ActorLogging { import InnerChild._ override def receive: Receive = { case TestMessage(msg) => log.info(s"Child received message: ${msg}") } }
object ParentalActor { case class SendToSupervisor(msg: InnerChild.TestMessage) case class SendToInnerChild(msg: InnerChild.TestMessage) case class SendToChildSelection(msg: InnerChild.TestMessage) def props = Props[ParentalActor] }
class ParentalActor extends Actor with ActorLogging { import ParentalActor._ val supervisor = context.actorOf(Supervisor.props, "supervisor") supervisor ! BackoffSupervisor.getCurrentChild var innerChild: Option[ActorRef] = None val selectedChild = context.actorSelection("/user/parent/supervisor/innerChild") override def receive: Receive = { case BackoffSupervisor.CurrentChild(ref) => innerChild = ref case SendToSupervisor(msg) => supervisor ! msg case SendToChildSelection(msg) => selectedChild ! msg case SendToInnerChild(msg) => innerChild foreach(child => child ! msg) } }
object BackoffSupervisorDemo extends App { import ParentalActor._ val testSystem = ActorSystem("testSystem") val parent = testSystem.actorOf(ParentalActor.props, "parent") Thread.sleep(1000) parent ! SendToSupervisor(TestMessage("Hello message 1 to supervisor")) parent ! SendToInnerChild(TestMessage("Hello message 2 to innerChild")) parent ! SendToChildSelection(TestMessage("Hello message 3 to selectedChild")) scala.io.StdIn.readLine() testSystem.terminate() } ```