2013-02-15 64 views

回答

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

選擇監護人的所有兒童,並將其發送的消息:

context.system.eventStream.subscribe 

我從延伸。

+0

問題是關於發送給所有演員,但不僅僅是根的孩子。關於根的孫子們(/ ...)呢? – 2013-03-26 14:55:19

8

如果你想將消息發送到誰是動態創建的所有演員,你可以使用eventBus

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

從一個演員,你可以發送給大家:

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

或直接與系統。

演員必須與訂閱:

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

謝謝twillouer!我一直在考慮這個問題,但我真的很想念那個例子。 – Pepster 2013-02-15 10:52:05

+0

超級有用,我會研究這個.. – Pepster 2013-02-15 11:24:41

+1

1.可能在'postStop'你需要「取消訂閱」; 2.取消訂閱_before_ calling'super.postStop()'可能更好。 – 2013-02-15 12:07:44