Is it possible to send a message to all actors in an actor system? I've been looking at the Broadcast router example, but that is so marginal I can't comprehend how I add actors to the router dynamically.
We are using scala for akka.
Thanks!
system.actorSelection("/user/*") ! msg
Selects all children of the guardian and sends them the msg.
If you want send a message to all actor who are dynamically created, you can use eventBus
I personally use the system.eventStream for my case.
From an actor, you can send to everyone :
context.system.eventStream.publish(StatisticsMessage())
or directly with system.
actor must subscribe with :
context.system.eventStream.subscribe
I extends from :
trait SubscriberActor extends Actor {
def subscribedClasses: Seq[Class[_]]
override def preStart() {
super.preStart()
subscribedClasses.foreach(this.context.system.eventStream.subscribe(this.self, _))
}
override def postStop() {
subscribedClasses.foreach(this.context.system.eventStream.unsubscribe(this.self, _))
super.postStop()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With