If you want to send a broadcast to all child actors of a given actor, a BalancingPool router is definitely not what you want, nor is a RoundRobin router.
Read the section "Pools vs Groups" in this link. Disregard the fact that it is a .NET doc; its content is platform-agnostic
If you use a routing pool, you don't hold references to the actors that get created by the routing pool. Therefore, unless you want to do some magic to figure out the actor path names, you can only send them messages using the routing logic of that routing pool.
What you want is to create the actors yourself and then supply them as routees when you create the routing group. Then you get to address them both directly and via the router.
If you want to send them all a message, you can just myActors foreach (_ ! "message")
, and if you want to go via router, you can router ! "message"
I'm afraid there is no "BalancingPool" router group equivalent; I'll give you a full example using the RoundRobin routing logic:
import akka.actor.{Actor, ActorSystem, Props}
import akka.routing.{ActorRefRoutee, RoundRobinRoutingLogic, Router}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
class ParentActor extends Actor {
val actors = (1 to 5).map(i => context.system.actorOf(Props[ChildActor], "child" + i))
val routees = actors map ActorRefRoutee
val router = Router(RoundRobinRoutingLogic(), routees)
router.route("hello", sender())
router.route("hello", sender())
router.route("hello", sender())
router.route("hello", sender())
router.route("hello", sender())
actors foreach (_ ! "broadcast")
def receive = { case _ => }
}
class ChildActor extends Actor {
def receive = {
case "hello" => println(s"${self.path.name} answers hey")
case "broadcast" => println(s"${self.path.name} received broadcast")
}
}
object Main extends App {
val system = ActorSystem("HelloSystem")
val parent = system.actorOf(Props(classOf[ParentActor]))
Future {
Thread.sleep(5000)
system.terminate()
}
}
Outputs when sbt run
[info] Running Main
child2 answers hey
child5 answers hey
child1 answers hey
child4 answers hey
child1 received broadcast
child3 answers hey
child4 received broadcast
child3 received broadcast
child5 received broadcast
child2 received broadcast
[success] Total time: 8 s, completed 7/05/2016 6:28:18 PM
>
Good luck learning Akka!