I am brand new to Akka and am trying to wrap my head around SupervisorStrategies
and child actor termination. I have several very similar concerns.
First off, my understanding is that if an actor creates another actor (via context.actorOf(...)
), then it is automatically the created actor's parent/supervisor. This is the only way an actor can be a parent of/supervisor of another actor.
If anything I have said above is incorrect, please begin by correcting me! But, assuming I'm more or less on track, then what happens when multiple parent actors create instances of the same child actor:
// Groovy pseudo-code.
class Fizz extends UntypedActor {
@Override
void onReceive(Object message) {
ActorRef buzz = context.system.actorOf(Props.create(Buzz), "buzz")
buzz.tell("Hello buzz!", self)
}
}
class Foo extends UntypedActor {
@Override
void onReceive(Object message) {
ActorRef buzz = context.system.actorOf(Props.create(Buzz), "buzz")
buzz.tell("Meh!", self)
}
}
In the above example, are Fizz
and Foo
parents to the same Buzz
actor instance, or two instances of Buzz
altogether? If it's multiple instances, can I assume that terminating one instance only terminates that instance? Or if Foo
terminates/stops its Buzz
instance, does that also terminate/stop Fizz
's instance?
First of all these two are different:
context.system.actorOf(Props.create(Buzz), "buzz")
context.actorOf(Props.create(Buzz), "buzz")
context.system.actorOf()
creates the top level user actor. In order to create child actor, you have to use context.actorOf()
. The rest of the answer will assume that you use the latter method.
Akka's actor supervision system is hierarichical. That means that each actor has only one parent. So your quesion:
are Fizz and Foo parents to the same Buzz actor instance,
is false, Buzz
actor created from Foo
, and Buzz
created from Fizz
, are different instance, which has different parent.
You can confirm it by viewing the path of the actor.
Buzz
created by Foo
has path something like akka://<system_name>/user/foo/buzz
, and Buzz
created by Fizz
has path something like akka://<system_name>/user/fizz/buzz
(I don't use java, but maybe you can get the actor path by something like getPath()
method of ActorRef
or Actor
)
Additionally the Buzz
actor created by context.system.actorOf()
has path something like akka://<system_name>/user/buzz
, which is directly supervied by the User Guardinan actor.
So the relation between the actor path is:
akka://<system_name>/user/fizz/buzz
(instace of Buzz
) is supervised by akka://<system_name>/user/fizz
(Instance of Fizz
)akka://<system_name>/user/foo/buzz
(another instance of Buzz
) is supervised by akka://<system_name>/user/foo
(instance of Foo
) .Therefore your main question:
can I assume that terminating one instance only terminates that instance? Or if Foo terminates/stops its Buzz instance, does that also terminate/stop Fizz's instance?
the former is correct.
Read akka docs for more detail: