Sending a message to all participants in the ActorSystem

Is it possible to send a message to all the actors in the actors' system? I watched the Broadcast router example, but it is so insignificant that I cannot figure out how to dynamically add participants to the router.

We use scala for akka.

Thanks!

+4
source share
2 answers
system.actorSelection("/user/*") ! msg 

Selects all guardian children and sends them a message.

+11
source

If you want to send a message to all the actors who are dynamically created, you can use eventBus

I personally use system.eventStream for my business.

From the actor you can send to everyone:

 context.system.eventStream.publish(StatisticsMessage()) 

or directly with the system.

The actor must subscribe to:

 context.system.eventStream.subscribe 

I continues 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() } } 
+8
source

All Articles