Creating actor using context vs system


(Mghildiy) #1

Hi,

Going through akka documentation, I came across two ways of creating actors:

system.actorOf(…)
getContext().actorOf(…)

How do they differ, and which to use when?


(Rob Crawford) #2

They differ on the final actor’s parent actor. Calling system.actorOf(…) creates a child of the user guardian, while getContext().actorOf(…) creates a child of the current actor. Note that getContext() is only available inside an actor, while the “system” reference is what you get when you create the actor system.

I tend to create a single actor using system.actorOf(…), then let it bootstrap the system by creating children to populate the actor system, but there’s no reason you couldn’t create multiple children of the user guardian instead.


(João Pedro Evangelista) #3

It also goes down to supervision trees, using getContext you create a child of the current actor and when the child needs it’s parent to work it makes sense while creating a parent and a child under the system actor would leave the child idle when it’s parent dies