本文介绍了ActorRef到其他演员好还是坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚我对于其他演员的使用​​情况并不是一种反模式。

I'm trying to figure out if my usage of passing Akka ActorRef around to other actors is not an anti-pattern.

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

I've a few actors in my system. Some are long lived (restClientRouter,publisher) and some die after that they have done the work (geoActor). The short-lived actors need to send messages to the long-lived actors and therefore need their 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 code> publisher )到 GeoInferenceActor 的构造函数。这可以吗?有更好的做法吗?

As you can see I'm passing the ActorRefs (restClientRouter and publisher) to the constructor of GeoInferenceActor. Is this okay or not? Is there a better way of doing this ?

推荐答案

有几种很好的方法来介绍演员参考需要他们的演员实例。

There are a couple of good ways to "introduce" actor refs to actor instances that need them.

1)使用构造函数args(这正是您正在做的)创建一个需要的参数的演员。

1) Create the actor with the refs it needs as constructor args (which is what you are doing)

2)创建实例后,传递所需的参考信息中的消息

2) Pass in the needed refs with a message after the instance is created

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

Your solution is perfectly acceptable, and even suggested by Roland Kuhn, the Tech Lead for Akka, in this post:

这篇关于ActorRef到其他演员好还是坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 09:18