我需要重新使用相同的Akka ActorSystem还是每次需要一个就创建一个? [英] Do I need to re-use the same Akka ActorSystem or can I just create one every time I need one?

查看:264
本文介绍了我需要重新使用相同的Akka ActorSystem还是每次需要一个就创建一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Akka 2.x需要许多命令来引用 ActorSystem 。因此,要创建actor MyActor 的实例,您可能会说:

Akka 2.x requires many commands to reference an ActorSystem. So, to create an instance of an actor MyActor you might say:

val system = ActorSystem()
val myActor = system.actorOf(Props[MyActor])

由于经常需要 ActorSystem :许多代码示例都省略了代码的创建,并假定读者知道 system 变量来自。

Because of the frequent need for an ActorSystem: many code examples omit the creation from the code and assume that the reader knows where a system variable has come from.

如果您的代码在不同位置产生actor,则可以复制此代码,可能会创建其他 ActorSystem 实例,或者您可以尝试通过引用某些全局变量或通过传递 ActorSystem 来共享相同的 ActorSystem 实例。

If your code produces actors in different places, you could duplicate this code, possibly creating additional ActorSystem instances, or you could try to share the same ActorSystem instance by referring to some global or by passing the ActorSystem around.

Akka文档提供了常规演员系统标题下的演员系统概述,其中有 ActorSystem 的文档。但是,这些都不对解释Akka用户为什么不能仅仅依靠Akka来管理这种后台功能有很大帮助。

The Akka documentation provides a general overview of systems of actors under the heading 'Actor Systems', and there is documentation of the ActorSystem class. But neither of these help a great deal in explaining why a user of Akka can't just rely on Akka to manage this under-the-hood.


  • 共享相同的 ActorSystem 对象或创建的含义是什么每次都有新的吗?

  • What are the implications of sharing the same ActorSystem object or creating a new one each time?

这里的最佳做法是什么?一直无所事事地绕过 ActorSystem

What are the best practices here? Passing around an ActorSystem all the time seems surprisingly heavy-handed.

某些示例给出了 ActorSystem 名称: ActorSystem( MySystem)其他人只需调用 ActorSystem()。这有什么区别?如果两次使用相同的名称,该怎么办?

Some examples give the ActorSystem a name: ActorSystem("MySystem") others just call ActorSystem(). What difference does this make, and what if you use the same name twice?

是否执行 akka-testkit 是否要求您与传递给 TestKit 构造函数的那个​​共享一个共同的 ActorSystem

Does akka-testkit require that you share a common ActorSystem with the one you pass to the TestKit constructor?

推荐答案

创建ActorSystem非常昂贵,因此您希望避免每次都创建一个新的ActorSystem需要它。而且,除非有充分的理由,您的演员应该在同一ActorSystem中运行。 ActorSystem的名称也是在其中运行的actor的路径的一部分。例如。如果您在名为 MySystem 的系统中创建一个actor,它将具有类似于 akka:// MySystem / user / $ a 。如果您在角色环境中,则始终有对ActorSystem的引用。在Actor中,您可以调用 context.system 。我不知道akka-testkit会有什么期望,但是您可以看一下akka测试。

Creating an ActorSystem is very expensive, so you want to avoid creating a new one each time you need it. Also your actors should run in the same ActorSystem, unless there is a good reason for them not to. The name of the ActorSystem is also part the the path to the actors that run in it. E.g. if you create an actor in a system named MySystem it will have a path like akka://MySystem/user/$a. If you are in an actor context, you always have a reference to the ActorSystem. In an Actor you can call context.system. I don't know what akka-testkit expects, but you could take a look at the akka tests.

因此,总结起来,应该始终使用同一系统,除非有充分的理由不这样做。

So to sum it up, you should always use the same system, unless there is a good reason not to do so.

这篇关于我需要重新使用相同的Akka ActorSystem还是每次需要一个就创建一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆