In akka the poststop method is called when an actor is stopped.
Also a watch can be configured on the supervisor so that when an actor is terminated a Terminated message is received within the supervisor.
Is there any advantage of using the postStop instead of Terminated? The only difference I can see is that postStop is called(within actor) when an actor is stopped and Terminated is called(within supervisor) when actor is Terminated.
When an actor is stopped is'nt it also terminated ?
First of all, postStop()
is called from preRestart()
default implementation of the same actor (see here), while Terminated
message is sent to the parent only when the actor is really stopped, not when it is just restarted. Hence in default setup postStop()
may be called more times than Terminated
message is sent.
But the main idea is that post*
and pre*
hooks are needed to cleanup the actor's own state, and Terminated
message (the whole watch system, in fact) is intended to coordinate multiple actors behavior and state. Their purpose is different, and while you can send custom Terminated
-like messages from postStop()
hook, you really shouldn't.
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