Being new to Akka I need help in understanding in a simple way what the benefit of Props is. What is the problem on having common OO style object creation?
What I know is that this follows the factory pattern where you send the class and the properties in a PROP to a factory and that creates the actor for you. [Correct me if im wrong]?
BUT I fail to see the need and I know that this is fundamental. This is my dilemma.
Can you please help me understand this may be by way of an analogy/code?
Props is a configuration object using in creating an Actor; it is immutable, so it is thread-safe and fully shareable.
A prop, formally known as (theatrical) property, is an object used on stage or screen by actors during a performance or screen production. In practical terms, a prop is considered to be anything movable or portable on a stage or a set, distinct from the actors, scenery, costumes, and electrical equipment.
Props is a configuration class which is used to specify options while creating an actor. It is immutable, so it is thread-safe and shareable. You can implement Props by importing akka.
What is an Actor in Akka? An actor is essentially nothing more than an object that receives messages and takes actions to handle them. It is decoupled from the source of the message and its only responsibility is to properly recognize the type of message it has received and take action accordingly.
I see two advantages to this way of creating actors.
The first one is simple: it gives you a guarantee that when an Actor
object is created, it's also properly registered in the actor system (it must have a parent actor to supervise it, gets pushed messages by the dispatcher, etc.). So you never end up with an object which is of type Actor
, but actually exists outside of the actor system.
The second one is visible in the definition of the actorOf(props: Props): ActorRef
method: it doesn't actually return an Actor
, but rather an ActorRef
(and the ActorRef
doesn't expose a reference to the underlying Actor
either).
This means that you never get direct access to the actor itself, and cannot circumvent the Akka API by directly calling methods on the actor, instead of sending async messages. If you built the Actor
yourself, you would obviously get direct access to the actor, making it way too easy to access it in ways which break the guarantees of the actor model.
The main reason for the usage of Props in Akka is that the lifecycle of the Actor
instances are completely managed by the ActorSystem
.
In the simplest case, your Actor
will be instantiated once, and then chug along happily. If that was the only use case, then a standard dependency injection approach would probably work.
However, that simplest case is thrown out the window once the Akka supervision mechanisms kicks in. In Akka, when an Actor
throws an Exception
, a supervision mechanism decides what should happen to that actor. Typically, the supervisor has to decided if:
Actor
instance is discarded but its mailbox is retained. A new, fresh Actor
instance is created from the Props
, and will start processing the original mailbox.The Props
mechanism is required for handling the restarting case.
I believe that the Props mechanism is also useful when creating a distributed ActorSystem
, as it allows instantiating the Actor
on another JVM if necessary.
All those cases cannot be handled by a standard factory pattern or by standard dependency injection mechanisms.
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