Akka parent-child Actors example
As we discussed in the previous sections, Akka's ActorSystem creates all top-level actors, which may act as parent or supervisor Actors. So, how do we create subordinate Actors?
When we execute the ActorSystem.actorOf()
function, it creates all top-level Actors. However, Akka Toolkit has provided an implicit variable, context of the ActorContext
type in the Actor
class, as follows:
trait Actor { implicit val context: ActorContext = { // Code goes here } }
Like ActorSystem.actorOf()
, the ActorSystem also has actorOf()
with the same set of parameters to create child or subordinate Actors from a supervisor or parent Actor:
package com.packt.publishing.supervision import akka.actor.{Actor,Props,ActorSystem} sealed trait Work case object DevWork extends Work case object TestingWork extends Work class LeadDeveloperActor extends Actor{ def receive = { case DevWork => val devActor = context.actorOf(Props[DevActor],"Dev") ...