9

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?

Arpan Patro
  • 133
  • 1
  • 7

3 Answers3

15

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.

Cyäegha
  • 4,043
  • 2
  • 19
  • 35
3

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.

LordOfThePigs
  • 10,378
  • 5
  • 41
  • 64
0

I don't think there is any advantage too. Actually, it's wired for me to understand the API.

why not like this:

ActorRef actorA = system.getRefByCreateActor(ActorA.class, Props.create(......));

NoSender.tell(actorA, message);
Sarabjit Singh
  • 1,738
  • 16
  • 27
Kenneth
  • 145
  • 1
  • 8