加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 绕过ActorRef到其他演员好还是坏?

发布时间:2020-12-16 09:37:35 所属栏目:安全 来源:网络整理
导读:我试图弄清楚,如果我将通过Akka ActorRef的使用传递给其他演员不是一个反模式。 我的系统中有几个演员。有些是长期居住的(restClientRouter,发布者),有些人已经完成了工作(geoActor)。短命的演员需要向长期的演员发送消息,因此需要他们的ActorRefs。 //r
我试图弄清楚,如果我将通过Akka ActorRef的使用传递给其他演员不是一个反模式。

我的系统中有几个演员。有些是长期居住的(restClientRouter,发布者),有些人已经完成了工作(geoActor)。短命的演员需要向长期的演员发送消息,因此需要他们的ActorRefs。

//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)使用构造函数args(这正是你正在做的)创建一个需要的参数的actor

2)创建实例后,使用消息传递所需的引用

您的解决方案是完全可以接受的,甚至可以由Akka的技术负责人Roland Kuhn在这篇文章中提出:

Akka actor lookup or dependency injection

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读