当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Identifying multiple instances of Akka Actors

问题描述:

I am learning Akka Actor programming (Java), and I am reading/watching several tutorials. 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?

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? 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? 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. 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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: