I am studying F# Akka.Net API which is quite different from its C# counterpart because it tries to expose Akka functionality in an idiomatic manner. So it introduces a computational expression actor and a couple of helper functions actorOf and actorOf2:
let consoleWriterActor =
spawn myActorSystem "consoleWriterActor" (actorOf Actors.consoleWriterActor)
let consoleReaderActor =
spawn myActorSystem "consoleReaderActor" (actorOf2 (Actors.consoleReaderActor consoleWriterActor))
The actorOf is useful to spawn an actor that don't depend on additional actors, and actorOf2 allows sending another actor as an argument for the actor being created as shown above.
This is clear, but I wonder what's so special with taking just a single actor as a parameter? Wouldn't defining actorOf3, actorOf4 etc. help to send multiple actor parameters to an actor being created? For example, a C# generic Action<...> macro has definitions for up to 16 action arguments. Would it make sense to define several actorOfX functions? For example, the code from Akka.Net bootcamp that uses validation action looks like this:
let consoleWriterActor =
spawn myActorSystem "consoleWriterActor" (actorOf Actors.consoleWriterActor)
let validationActor =
spawn myActorSystem "validationActor" (actorOf2 (Actors.validationActor consoleWriterActor))
let consoleReaderActor =
spawn myActorSystem "consoleReaderActor" (actorOf2 (Actors.consoleReaderActor validationActor))
You can see that actors reference each other in chain: consoleReaderActor references validationActor and validationActor references consoleWriterActor. But what if I want consoleReaderActor reference both validationActor and consoleWriterActor? It doesn't look like this is possible using actorOf functions.
UPDATE Thanks to the answer from @Horusiath. I checked one of the lessons from Akka.Net bootcamp (F# version), and it is in fact possible to specify multiple actors when spawning a new one:
let a1 =
spawn myActorSystem "a1" (actorOf Actors.consoleWriterActor)
let a2 =
spawn myActorSystem "a2" (actorOf2 (Actors.consoleReaderActor consoleWriterActor))
let a2 =
spawn myActorSystem "a3" (actorOf2 (Actors.fileValidatorActor consoleWriterActor tailCoordinatorActor))
actorOf2
is not sending any actors as an argument. Instead it exposes actor context to the user-defined function. While for simple sink-based actors it's not necessary - here actorOf
is sufficient - it's required for any kind of more advanced work, like spawning child actors, scheduling and subscribing and publishing messages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With