Creating a BroadcastPool of actors
In some situations, we may want to send the same message to all the actors. Here is a BroadcastPool
that lets us do so. It can be used when we want to send a common command to all the actors to do the same work.
Getting ready
Just import the Hello-Akka
project in the IDE; the other prerequisites are the same as before.
How to do it...
- Create a Scala file,
Broadcastpool.scala
, in the packagecom.packt.chapter3
. - Add the following imports to the top of the file:
import akka.actor.{Props, ActorSystem, Actor} import akka.routing.BroadcastPool
- Define a simple actor as an example:
class BroadcastPoolActor extends Actor { override def receive = { case msg: String => println(s" $msg, I am ${self.path.name}") case _ => println(s" I don't understand the message") } }
- Create a simple test application as follows:
object Broadcastpool extends App {
val actorSystem...