system.actorSelection("/user/*") ! msg
选择所有父元素的子元素并向它们发送消息。
如果您想发送消息给所有动态创建的actor,可以使用eventBus
我个人在我的情况下使用系统.eventStream。
从一个actor中,您可以向所有人发送:
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()
}
}
postStop
中你需要 "unsubscribe"。super.postStop()
之前取消订阅可能是一个更好的主意。