簡體   English   中英

向ActorSystem中的所有actor發送消息

[英]Sending a message to all actors within an ActorSystem

是否可以向演員系統中的所有演員發送消息? 我一直在看廣播路由器的例子,但是這太邊緣了,我無法理解我是如何動態地將actor添加到路由器的。

我們正在使用scala for akka。

謝謝!

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

選擇監護人的所有孩子並向他們發送消息。

如果要向動態創建的所有actor發送消息,可以使用eventBus

我個人使用system.eventStream來處理我的情況。

從演員那里,你可以發送給每個人:

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

或直接與系統。

演員必須訂閱:

context.system.eventStream.subscribe

我延伸自:

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()
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM