concept other actor in category akka

appears as: other actors, other actors, other actor
Reactive Applications with Akka .NET

This is an excerpt from Manning's book Reactive Applications with Akka .NET.

Although Akka.NET itself and how it works might be new to many developers, its underlying principles have been in development for decades, in the form of the actor model. As part of the actor model, independent entities, known as actors, are responsible for performing work. You can have multiple different types of actors within a system, and each of these types can have multiple instances running in the system. Every actor runs independently of every other actor in the system, meaning that two running actors can’t directly interfere or interact with each other. Instead, each actor is supplied with a mailbox, which receives messages, and an address, which can be used to receive messages from other actors in the system. An actor sits idle and doesn’t do anything until a new message is received in the actor’s mailbox; at this point, the actor can process the message using its internal behavior. Its behavior is the brain of the actor and defines how it should respond to each message it receives. If an actor receives more than one message, the messages are queued up in the order in which they were received, and the actor processes each message sequentially. Each actor will only process a single message at a time, although multiple actors can process their respective messages at the same time. This allows you to create highly concurrent applications without having to concern yourself with the underlying multithreading infrastructure and code that’s typically required when developing concurrent applications. It’s important to note also that actors are completely isolated, meaning that any internal information or data owned by one actor isn’t accessible by anything other than that actor.

You’ve seen how an actor can be created by calling ActorOf in the framework, which will cause an actor to be instantiated. You’ve also seen how an actor can be killed either programmatically by itself or by other actors, or by the framework when it detects a failure. Actors in Akka.NET operate and transition through a number of states during their lifecycle. Figure 6.7 shows the states an actor can exist in, as well as how to move between those states.

6.2.5. Watching for the deaths of other actors

In Akka.NET, it’s not just the parent of an actor that can watch for its failure; through a concept known as DeathWatch, other actors can monitor an actor and be notified if it fails. Using DeathWatch, when the framework discovers that an actor has failed, it sends a message to all actors who have subscribed to that notification.

The DeathWatch concept outlined earlier can be used here. You can create an actor whose job it is to DeathWatch each of the core actors you’re interested in (the reaper pattern). Once the reaper actor gets a message informing it of the death of all the core actors, then it’s safe to pass the barrier. You can see the reaper pattern illustrated in figure 6.10. The regular hierarchy of actors is positioned below the parent actor, but you also see the reaper actor, waiting for the deaths of the other actors.

Figure 6.10. The reaper pattern is an application of DeathWatch that allows you to know when all actors have finished working.

You can create a reaper actor that watches other actors and then safely executes operations once all their work is complete. The reaper actor takes a collection of actor paths that it watches for deaths. It then subscribes to DeathWatch notifications for all of these actors, before listening for the terminated events. When it receives a -Terminated message, it removes that actor from the collection of watched actors and checks to see whether it needs to wait for any other actors. If it doesn’t, then it exits the barrier.