简体   繁体   English

识别Akka Actors的多个实例

[英]Identifying multiple instances of Akka Actors

I am learning Akka Actor programming (Java), and I am reading/watching several tutorials. 我正在学习Akka Actor编程(Java),我正在阅读/观看几个教程。 I think I grasp most of it. 我想我掌握了大部分内容。

However one thing puzzles me, and sorry if this is a Noob question, but it is about having multiple instances of Actors, and how to create/retrieve/identify them? 然而有一件事让我感到困惑,如果这是一个Noob问题,对不起,但它是关于有多个Actors实例,以及如何创建/检索/识别它们?

Lets say I have an Account Actor, and want multiple instances of it. 假设我有一个帐户角色,并想要它的多个实例。 Am I supposed to keep a record of "account numbers" and a reference to the appropriate Actor instance? 我应该记录“帐号”和对相应的Actor实例的引用吗? Like a 像一个

Map<String,ActorRef> 

or something, stored in the Supervisor/Parent Actor? 或者什么,存储在主管/家长中?

At startup, should I then create one Actor per account, or should I have a factory that creates the Account Actor when its is needed the first time? 在启动时,我应该为每个帐户创建一个Actor,还是应该在第一次需要时创建一个创建Account Actor的工厂? And what about persistance? 那么持久性呢? Lets say the Account want to load a list of transactions that have been applied? 假设帐户想要加载已应用的交易列表?

Should I code all this manually, or is there a smarter "best practice" here which I dont understand? 我应该手动编码所有这些,还是有一个更聪明的“最佳实践”,我不明白?

In your case, you could simply create persistent actors under the supervisor where persistence id would correspond to the account number. 在您的情况下,您可以简单地在主管下创建持久性actor ,其中持久性ID将对应于帐号。 Therefore, account number can be used as a unique identifier. 因此,帐号可以用作唯一标识符。 To obtain actors from context of supervisor, you can use ActorContext#child method or simply to recover them from persistent store by using ActorContext#actorOf if they are not in the current context of the supervisor. 要从主管的上下文中获取actor,您可以使用ActorContext #child方法,或者只是通过使用ActorContext #actorOf从持久存储中恢复它们,如果它们不在主管的当前上下文中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM