Is there an elegant way of doing a hard restart of an actor - i.e. clearing out the mailbox along with internal state?
I know it can be done by calling context.stop
and reinitializing upon the DeathWatch / Terminated
message, but that's a bit clunky.
No, clearing out the mailbox is exactly what is done by terminating the actor. If you were to try that without the termination semantics, how could you ever be sure that you cleared everything? New messages could come in at any point in time.
So, to do that hard restart you
- return the
Stop
directive from the supervisor strategy
- then create a new child once you receive that actor’s
Terminated
message.