我想弄清楚我是否使用了传递 Akka ActorRef
周围的其他 Actor 不是一种反模式。
我的系统中有几个 Actor 。有些人很长寿( restClientRouter
, publisher
),有些人在完成工作后死去( geoActor
)。短期 Actor 需要向长期 Actor 发送消息,因此需要他们的ActorRef
s。
//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")
如您所见,我将 ActorRefs(
restClientRouter
和 publisher
)传递给 GeoInferenceActor
的构造函数.这好吗?有没有更好的方法来做到这一点?
最佳答案
有几种好方法可以将角色引用“引入”到需要它们的角色实例。
1) 使用它需要的 refs 作为构造函数 args 创建actor(这就是你正在做的)
2)在实例创建后通过消息传入所需的引用
您的解决方案是完全可以接受的,甚至是 Akka 的技术主管 Roland Kuhn 在这篇文章中提出的建议:
Akka actor lookup or dependency injection
关于scala - 将 ActorRef 传递给其他 Actor 是好是坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24840608/