ActorRef到其他演员好还是坏?

编程入门 行业动态 更新时间:2024-10-12 03:16:23
本文介绍了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:

Akka演员查找或依赖注入

更多推荐

ActorRef到其他演员好还是坏?

本文发布于:2023-11-25 02:33:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:演员   ActorRef

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!