Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending a message to all actors within an ActorSystem

Tags:

scala

akka

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!

like image 616
Pepster Avatar asked Feb 15 '13 10:02

Pepster


2 Answers

system.actorSelection("/user/*") ! msg

Selects all children of the guardian and sends them the msg.

like image 151
agilesteel Avatar answered Nov 20 '22 02:11

agilesteel


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()
  }
}
like image 25
twillouer Avatar answered Nov 20 '22 02:11

twillouer