Reputation: 34061
I have the following scenario, that looks as the following:
I would like to send messages from DetectorFSM to AppTriggerSupervisor(the blue solid line) and the question is, how to design it correctly.
What is the best way to get the reference of AppTriggerSupervisor on actor DetectorFSM? Via Path or Reference? If via Reference, then I have to get the reference of AppTriggerSupervisor somehow(pass the reference during initialization?).
Or should I send messages from DetectorFSM to DetectorSupervisor first(the red solid line) and then from there send it to AppTriggerSupervisor?
But the main question is, I am searching the best practice to get references from actors, that are not on the same node. For example, like AppTriggerSupervisor and DetectorSupervisor on the picture above. AppTriggerSupervisor and DetectorSupervisor do not know from each other, but I would like to exchange messages between them, so how to get to know each other?
Upvotes: 4
Views: 160
Reputation: 4800
From the docs:
It is always preferable to communicate with other Actors using their ActorRef instead of relying upon ActorSelection. Exceptions are
- sending messages using the At-Least-Once Delivery facility
- initiating first contact with a remote system
In all other cases ActorRefs can be provided during Actor creation or initialization, passing them from parent to child or introducing Actors by sending their ActorRefs to other Actors within messages.
For example. you say that AppTriggerSupervisor and DetectorSupervisor do not know each other, but they (indirectly) do. They both have the same parent IT-System. AppTriggerSupervisor can message its parent with "please give me the ActorRef of the DetectorSupervisor". Or, perhaps better yet, "please forward this message to whomever is best able to handle it". This isolates you from any future changes in the Actor Hierarchy and gives a lot of flexibility.
Also, ActorRefs indicate that there is (or at least was) a valid actor. An ActorPath? It's hard to know, the best thing to do is to send an Identify message so you can convert it to an ActorRef.
If you use ActorPaths then you will have to incorporate the information about where the Actor the actor is running, assuming that a remote location is possible. By which I mean that if you are using ActorPath to "discover" your actors, then you are either assuming it is local (a bad assumption, for a system designed with location independence it mind) or you are having to abstract the network topology somehow.
Additionally, using ActorPaths can make testing more difficult because the actor is less encapsulated. Not impossible, of course, but it means you'll have to know which paths the actor under testing might communicate with and then start mock Actors on those paths. Whereas, if the actor is using either Props or its parent to locate ActorRefs indirectly, it's easier to substitute probes.
I feel like there probably is another exception or two to the recommendation above, there are some nice features to ActorSelection/ActorPaths, but I would strongly default to discovering ActorRefs either from Props or via messages.
Upvotes: 1