Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between poststop and terminate in Akka?

Tags:

java

scala

akka

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 ?

like image 530
blue-sky Avatar asked Dec 19 '22 15:12

blue-sky


1 Answers

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.

like image 107
Vladimir Matveev Avatar answered Jan 11 '23 22:01

Vladimir Matveev