Safely shutting down a persistent actor
Shutting down an actor is often achieved by sending a PoisonPill
message to it. This allows the previous messages in the mailbox to get processed by the actor before it is stopped. Although this is totally correct for regular actors, it can turn into unexpected behaviors when using a persistent actor. Incoming commands are stashed while the persistent actor is awaiting confirmation from the journal that events have been stored when calling persist()
. The incoming commands will be drained from the Actor's mailbox and put into its internal stash while awaiting the confirmation (thus, before calling the persist
handlers). Therefore, the persistent actor may receive and (auto) handle the PoisonPill
message before it processes the other messages, causing a premature (and maybe unexpected) shutdown of the actor. In this recipe, we will look at how we should correctly shut down a persistent actor.
Getting ready
To step through this recipe, we need to import...