Question

import akka.actor._
case object ChildMessage

implicit val as = ActorSystem()

class Child extends Actor {
  def receive = {
    case ChildMessage => println("I'm a child")
  }
}

class ParentWithExplicitChildren extends Actor {
  val children = Array.fill(5)(context.actorOf(Props[Child]))
  def receive = {
    case ChildMessage => children.foreach(_ ! ChildMessage)
    case _ => println("I'm a parent")
  }
}

class ParentWithActorRefs extends Actor {
  val shamChildren = Array.fill(5)(as.actorOf(Props[Child]))
  def receive = {
    case ChildMessage => shamChildren.foreach(_ ! ChildMessage)
    case _ => println("I'm a parent")
  }
}

val parent = as.actorOf(Props[ParentWithExplicitChildren])
parent ! ChildMessage
// Will shut down children
parent ! PoisonPill


val shamParent = as.actorOf(Props[ParentWithActorRefs])
shamParent ! ChildMessage
// WONT shut down children
shamParent ! PoisonPill

Using the example above I can only think of two consequences of not having an explicit Parent Child relationship.

  1. Poison Pill won't explicitly kill the actor refs contained in ParentWithActorRefs
  2. ParentWithActorRefs's context.children will be empty

Are they other consequences? Does the non-child message relaying potentially have different message ordering semantics than the child message relaying? Can I not access ParentWithExplictChildren's child actor refs with actorSelection?

Was it helpful?

Solution

Your first two consequences are correct. You missed one though in the fact that when ParentWithExplicitChildren fails itself, it will stop and then start again all of it's children because it's the explicit supervisor for those children. In the case of ParentWithActorRefs, a failure in this actor will not stop the shamChildren because it's not their supervisor; the root guardian is.

Also, yes, you can access the children of ParentWithExplicitChildren refs via actor selection. They are proper actors with addressable paths and thus can be looked up and communicated with from outside of their parent/supervising actor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top