Sou*_*nta 26 design-patterns scala anti-patterns actor akka
我试图弄清楚我将Akka传递ActorRef给其他演员的用法是不是反模式.
我的系统里有几个演员.有些人长寿(restClientRouter,publisher),有些人在完成工作后死亡(geoActor).短命的演员需要向长寿演员发送信息,因此需要他们ActorRef的.
//router for a bunch of other actors
val restClientRouter = createRouter(context.system)
//publishers messages to an output message queue
val publisher: ActorRef = context.actorOf(Props(new PublisherActor(host, channel)), name = "pub-actor")
//this actor send a message to the restClientRouter and then sends the response
//to the publisher
val geoActor = context.actorOf(Props(new GeoInferenceActor(restClientRouter, publisher)), name = "geo-inference-actor")
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我将ActorRefs(restClientRouter和publisher)传递给了构造函数GeoInferenceActor.这样可以吗?有没有更好的方法呢?
cmb*_*ter 23
有几种很好的方法可以将actor引入"需要它们的actor实例".
1)使用构造函数args需要的refs创建actor(这是你正在做的)
2)在创建实例后,使用消息传入所需的引用
您的解决方案是完全可以接受的,甚至由Akka的技术负责人Roland Kuhn在这篇文章中建议:
Jea*_*art 17
如Akka API文档中所述,它完全有效:
ActorRefs可以通过消息传递在actor之间自由共享.
在你的情况下,你将它们传递给构造函数,这绝对是好的,它应该是这样的.