Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What benefit do Props bring to actor creation in Akka?

Tags:

prop

akka

actor

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?

like image 976
Arpan Patro Avatar asked Dec 15 '16 08:12

Arpan Patro


People also ask

What is the use of Akka props?

Props is a configuration object using in creating an Actor; it is immutable, so it is thread-safe and fully shareable.

What is props in actor?

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.

What is Akka props?

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.

How do actors work in 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.


2 Answers

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.

like image 159
Cyäegha Avatar answered Sep 24 '22 20:09

Cyäegha


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:

  • The exception is expected and doesn't really cause a problem => The actor is resumed and continues operating as normal.
  • The exception is fatal, and the actor cannot be allowed to continue => The actor is killed, its mailbox is destroyed and all of the messages it contains are sent to the dead letters.
  • The exception is bad but recoverable => The actor is restarted. This means that the existing 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.

like image 44
LordOfThePigs Avatar answered Sep 22 '22 20:09

LordOfThePigs