Class EventSourcedBehavior<Command,Event,State>
- Direct Known Subclasses:
EventSourcedBehaviorWithEnforcedReplies
,ReplicatedEventSourcedBehavior
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.pekko.actor.typed.Behavior
Behavior.BehaviorDecorators<Inner>, Behavior.BehaviorDecorators$
-
Constructor Summary
ConstructorsConstructorDescriptionEventSourcedBehavior
(PersistenceId persistenceId) EventSourcedBehavior
(PersistenceId persistenceId, BackoffSupervisorStrategy onPersistFailure) If using onPersistFailure the supervision is only around the event sourced behavior not any outer setup/withTimers block. -
Method Summary
Modifier and TypeMethodDescriptionapply
(TypedActorContext<Command> context) INTERNAL API: DeferredBehavior init, not for user extensionprotected abstract CommandHandler<Command,
Event, State> Implement by handling incoming commands and return anEffect()
to persist or signal other effects of the command handling such as stopping the behavior or others.protected final EffectFactories<Event,
State> Effect()
Factory of effects.protected abstract State
Implement by returning the initial empty state object.Transform the event in another type before giving to the journal.protected abstract EventHandler<State,
Event> Implement by applying the event to the current state in order to return a new state.Optional<com.typesafe.config.Config>
Override and define the journal plugin config that this actor should use instead of the default.Override and define the journal plugin id that this actor should use instead of the default.final long
lastSequenceNumber
(ActorContext<?> ctx) The last sequence number that was persisted, can only be called from inside the handlers of anEventSourcedBehavior
protected CommandHandlerBuilder<Command,
Event, State> protected final EventHandlerBuilder<State,
Event> protected final SignalHandlerBuilder<State>
recovery()
Override to change the strategy for recovery of snapshots and events.Criteria for retention/deletion of snapshots and events.boolean
shouldSnapshot
(State state, Event event, long sequenceNr) Initiates a snapshot if the given predicate evaluates to true.protected SignalHandler<State>
Override to react on general lifecycle signals and persistence specific signals (subtypes ofpekko.persistence.typed.EventSourcedSignal
).Transform the state into another type before giving it to and from the journal.Optional<com.typesafe.config.Config>
Override and define the snapshot store plugin config that this actor should use instead of the default.Override and define the snapshot store plugin id that this actor should use instead of the default.Thetagger
function should give event tags, which will be used in persistence queryMethods inherited from class org.apache.pekko.actor.typed.Behavior
BehaviorDecorators, canonicalize, existsInStack, interpretMessage, interpretSignal, isAlive, isDeferred, isUnhandled, narrow, start, validateAsInitial
-
Constructor Details
-
EventSourcedBehavior
- Parameters:
persistenceId
- stable unique identifier for the event sourced behavior
-
EventSourcedBehavior
public EventSourcedBehavior(PersistenceId persistenceId, BackoffSupervisorStrategy onPersistFailure) If using onPersistFailure the supervision is only around the event sourced behavior not any outer setup/withTimers block. If using restart any actions e.g. scheduling timers, can be done on the PreRestart signal or on the RecoveryCompleted signal.- Parameters:
persistenceId
- stable unique identifier for the event sourced behavioronPersistFailure
- BackoffSupervisionStrategy for persist failures
-
-
Method Details
-
Effect
Factory of effects.Return effects from your handlers in order to instruct persistence on how to act on the incoming message (i.e. persist events).
-
apply
INTERNAL API: DeferredBehavior init, not for user extension- Specified by:
apply
in classBehaviorImpl.DeferredBehavior<Command>
-
commandHandler
Implement by handling incoming commands and return anEffect()
to persist or signal other effects of the command handling such as stopping the behavior or others.Use
newCommandHandlerBuilder()
to define the command handlers.The command handlers are only invoked when the actor is running (i.e. not replaying). While the actor is persisting events, the incoming messages are stashed and only delivered to the handler once persisting them has completed.
-
emptyState
Implement by returning the initial empty state object. This object will be passed into this behaviors handlers, until a new state replaces it.Also known as "zero state" or "neutral state".
-
eventAdapter
Transform the event in another type before giving to the journal. Can be used to wrap events in types Journals understand but is of a different type thanEvent
. -
eventHandler
Implement by applying the event to the current state in order to return a new state.Use
newEventHandlerBuilder()
to define the event handlers.The event handlers are invoked during recovery as well as running operation of this behavior, in order to keep updating the state state.
For that reason it is strongly discouraged to perform side-effects in this handler; Side effects should be executed in
thenRun
orrecoveryCompleted
blocks. -
journalPluginConfig
Override and define the journal plugin config that this actor should use instead of the default. -
journalPluginId
Override and define the journal plugin id that this actor should use instead of the default. -
lastSequenceNumber
The last sequence number that was persisted, can only be called from inside the handlers of anEventSourcedBehavior
-
newCommandHandlerBuilder
- Returns:
- A new, mutable, command handler builder
-
newEventHandlerBuilder
- Returns:
- A new, mutable, event handler builder
-
newSignalHandlerBuilder
- Returns:
- A new, mutable signal handler builder
-
persistenceId
-
recovery
Override to change the strategy for recovery of snapshots and events. By default, snapshots and events are recovered. -
retentionCriteria
Criteria for retention/deletion of snapshots and events. By default, retention is disabled and snapshots are not saved and deleted automatically. -
shouldSnapshot
Initiates a snapshot if the given predicate evaluates to true.Decide to store a snapshot based on the State, Event and sequenceNr when the event has been successfully persisted.
When persisting multiple events at once the snapshot is triggered after all the events have been persisted.
Snapshots triggered by
snapshotWhen
will not trigger deletes of old snapshots and events ifEventSourcedBehavior.retentionCriteria
withRetentionCriteria.snapshotEvery
is used together withshouldSnapshot
. Such deletes are only triggered by snapshots matching thenumberOfEvents
in theRetentionCriteria
.- Returns:
true
if snapshot should be saved at the givenstate
,event
andsequenceNr
when the event has been successfully persisted
-
signalHandler
Override to react on general lifecycle signals and persistence specific signals (subtypes ofpekko.persistence.typed.EventSourcedSignal
).Use
newSignalHandlerBuilder()
to define the signal handler. -
snapshotAdapter
Transform the state into another type before giving it to and from the journal. Can be used to migrate from different state types e.g. when migration from PersistentFSM to Typed EventSourcedBehavior. -
snapshotPluginConfig
Override and define the snapshot store plugin config that this actor should use instead of the default. -
snapshotPluginId
Override and define the snapshot store plugin id that this actor should use instead of the default. -
tagsFor
Thetagger
function should give event tags, which will be used in persistence query
-