Hi Ian,

On Mon, Dec 29, 2014 at 10:19 PM, Ian Holsman <i...@holsman.com.au> wrote:

> Hi.
>
> I was wondering what the best practice was for finding a running
> instance of an certain class of actor. (say 'bank account')
>
> Currently I have a BankAccountManager which creates a new bank
> account, and uses a map to store my reference # with the actorRef.
>
> I then use a 'GetAccountById' message to lookup the actorRef  on the
> BankAccountManager actor and pass it back to the caller. who then
> interacts with the bank account. (which seems pretty Kludgy to me).
>
>
Yes, that is one way of doing it.

Another way is to send all messages to the bank accounts via the parent
BankAccountManager.


>
> I recently came across the 'context.actorSelection' call, and was
> wondering if I should be using this instead to find it, and if this is
> the preferred way of dealing with finding actors.
>

That would work in the above example, if the BankAccountManager knows and
creates all bank accounts up front.
In this case the names of the bank account actors can probably be well
known (and stable), e.g. using bank account number.

In general, using actor selection too much can make it difficult to
refactor and test the code. The actor paths changes with the supervision
hierarchy. Normally it is better to inject (constructor) ActorRefs to
dependent actors, or pass  ActorRefs in messages.


>
> ie. never hold-on to ActorRefs, and just use actorSelection to find them.
>

Note that ActorSelection is not as powerful as ActorRef. You can't watch an
ActorSelection. Sending messages via an ActorSelection is also slightly
less efficient than sending them directly with an ActorRef.



> or is there a better pattern I should be using.
>

I would probably start with sending all messages via the parent
BankAccountManager.
That also has a nice migration path into Cluster Sharding, if needed later.

Cheers,
Patrik


>
> Thanks!
> Ian
>
>
> --
> Ian Holsman
> i...@holsman.com.au
> PH: + 61-3-9028 8133 / +1-(425) 998-7083
>
> --
> >>>>>>>>>>      Read the docs: http://akka.io/docs/
> >>>>>>>>>>      Check the FAQ:
> http://doc.akka.io/docs/akka/current/additional/faq.html
> >>>>>>>>>>      Search the archives:
> https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to akka-user+unsubscr...@googlegroups.com.
> To post to this group, send email to akka-user@googlegroups.com.
> Visit this group at http://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>



-- 

Patrik Nordwall
Typesafe <http://typesafe.com/> -  Reactive apps on the JVM
Twitter: @patriknw

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to