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
选择监护人的所有孩子并向他们发送消息。
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()
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.