我想弄清楚我是否使用传递 AkkaActorRef
围绕其他参与者并不是一种反模式。
我的系统中有一些演员。有些寿命很长(restClientRouter
,publisher
),有些人在完成工作后就死了(geoActor
)。短命参与者需要向长命参与者发送消息,因此需要他们的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
and publisher
) 的构造函数GeoInferenceActor
。这可以吗?有更好的方法吗?
有几种好方法可以将 Actor 引用“引入”需要它们的 Actor 实例。
1)使用它需要的引用作为构造函数参数创建演员(这就是你正在做的)
2)实例创建后,通过消息传入需要的refs
您的解决方案是完全可以接受的,甚至是 Akka 技术主管 Roland Kuhn 在这篇文章中建议的:
Akka actor 查找或依赖注入 https://stackoverflow.com/questions/17770900/akka-actor-lookup-or-dependency-injection/17775684#17775684
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)