My favorite part about teaching Akka.NET to people is the questions I get. I never have any clue what someone is going to ask me, and that’s kind of exciting.
Last Thursday, we had an awesome group of developers participate in our advanced Akka.NET Design Patterns training for half the day. We covered a ton of material, but in that time, there was a basic question that came up:
When should I use an
I loved this question, because there is more nuance to the seemingly mundane
ActorSelection than meets the eye.
When Should I Use An
The guideline I follow is to use an
- I need to take advantage of wildcard selection in actor paths for some reason.
- I need to communicate with an actor on a remote actor system, and I don’t have an actor reference for it yet.
The first case when I usually use an
ActorSelection is when I need to leverage wildcards in my actor paths. This is pretty rare, but one example could be if I needed to distribute a message to a number of actors who all lived at the same level in the hierarchy, or had a well-defined and predictable
For example, let’s imagine I had an
OrderActor and needed to report on new orders to finance, marketing, and fulfillment. Let’s imagine that each of those concerns has its own hierarchy, with a
NewOrderActor at the second level that is the ingestion point for new orders. It would look like this:
In this case, I could send the new order messages on to all three
NewOrderActors with one wildcard
ActorSelection, like so: