scala - 将 ActorRef 传递给其他 Actor 是好是坏?

标签 scala design-patterns akka actor anti-patterns

我想弄清楚我是否使用了传递 Akka ActorRef周围的其他 Actor 不是一种反模式。

我的系统中有几个 Actor 。有些人很长寿( restClientRouterpublisher ),有些人在完成工作后死去( 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( restClientRouterpublisher )传递给 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/

相关文章:

akka - 在Akka.NET中,(使用Akka.Cluster)如何配置多个种子节点(Lighthouse)相互了解?

scala - SBT 中使用的 `addCommandAlias` 方法中的第一个分号是什么?

java - 当我将 scala nature 添加到 Eclipse 中的项目时收到错误

scala - 使用 DecisionTreeModel Spark ML 保存管道

json - 在 akka-http 中处理(序列化,实际上..)DeserializationException

java - 如何向 ShardRegion 中的所有 Actor 发送消息

arrays - Scala,数组总是按名称调用,对吧?

design-patterns - 为什么STL中没有 "Iterable"接口(interface)?

Java观察者和可观察者

java - 使用 Spring 或单独的库/模式实现数据库访问?