posthumecaver
posthumecaver

Reputation: 1863

Is it possible to define several stop messages in Akka Clustering

I am trying to configure an Akka Actor for Cluster Sharding, one thing that I am not quite sure, is it possible to configure several Stop Messages for an Entity for graceful Shutdown.

for ex, Entity configuration like following will trigger graceful shutdown for both 'onDelete' and 'onExit' or it will do it only for 'onExit'?

sharding
   .init(
       Entity(Actor1Key) {
          context => ....
       }
   )
   .withStopMessage(Actor1.onDelete)
   .withStopMessage(Actor1.onExit)

if not do you have any idea how I can achieve this Behaviour?

Thx for answers

Upvotes: 0

Views: 151

Answers (2)

johanandren
johanandren

Reputation: 11479

I think there may some confusion around what the purpose of the stopMessage is. There should not be a need for multiple stop messages.

The stopMessage sent by sharding after passivation has been requested by the actor, which is done by sending Passivate from the sharded actor itself.

You can let any of the messages that the actor accepts trigger passivation, the shard will send back the stopMessage when it is safe for the actor to actually stop.

The reason you should passivate rather than just Behaviors.stopped the actor is that there may be messages that was en route to the actor (mailbox and I think possibly a buffer in the shard in some circumstances) before the message causing it deciding to stop and you want to process those first. Passivation allows for that to happen by including a roundtrip to the shard actor which is charge of routing messages to the sharded actor.

A bit more details in the docs here: https://doc.akka.io/docs/akka/current/typed/cluster-sharding.html#passivation

Upvotes: 1

Anton Sarov
Anton Sarov

Reputation: 3748

What you have specified would only trigger the stop message for Actor1.onExit. The reason is how a stop message is defined for an Entity:

val stopMessage: Optional[M],

So you see that this is a plain optional thus no multiple elements are possible. You can also check how the withStopMessage is implemented here:

def withStopMessage(newStopMessage: M): Entity[M, E] =
    copy(stopMessage = Optional.ofNullable(newStopMessage))

So you are basically going to "overwrite" the message any time you call withStopMessage. Unfortunately, I am not aware of any other way of specifying multiple stop messages (besides combining multiple messages in a common trait but I think this is not what you are looking for).

Upvotes: 0

Related Questions