简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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