Class EventSourcedOnCommandWithReplyBehavior<Command,Event,State>
- java.lang.Object
-
- akka.actor.typed.Behavior<T>
-
- akka.actor.typed.internal.BehaviorImpl.DeferredBehavior<Command>
-
- akka.persistence.typed.javadsl.EventSourcedOnCommandWithReplyBehavior<Command,Event,State>
-
- Direct Known Subclasses:
ReplicatedEventSourcedOnCommandWithReplyBehavior
public abstract class EventSourcedOnCommandWithReplyBehavior<Command,Event,State> extends BehaviorImpl.DeferredBehavior<Command>
Event sourced behavior for projects built with Java 17 or newer where message handling can be done using switch pattern match.Enforces replies to every received command.
For building event sourced actors with Java versions before 17, see
EventSourcedBehavior
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class akka.actor.typed.Behavior
Behavior.BehaviorDecorators<Inner>, Behavior.BehaviorDecorators$
-
-
Constructor Summary
Constructors Constructor Description EventSourcedOnCommandWithReplyBehavior(PersistenceId persistenceId)
EventSourcedOnCommandWithReplyBehavior(PersistenceId persistenceId, BackoffSupervisorStrategy onPersistFailure)
If using onPersistFailure the supervision is only around the event sourced behavior not any outer setup/withTimers block.EventSourcedOnCommandWithReplyBehavior(PersistenceId persistenceId, java.util.Optional<BackoffSupervisorStrategy> onPersistFailure)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Behavior<Command>
apply(TypedActorContext<Command> context)
INTERNAL API: DeferredBehavior init, not for user extensionboolean
deleteEventsOnSnapshot()
Can be used to delete events aftershouldSnapshot
.protected EffectFactories<Event,State>
Effect()
Factory of effects.protected abstract State
emptyState()
Implement by returning the initial empty state object.EventAdapter<Event,?>
eventAdapter()
Transform the event in another type before giving to the journal.java.lang.String
journalPluginId()
Override and define the journal plugin id that this actor should use instead of the default.long
lastSequenceNumber(ActorContext<?> ctx)
The last sequence number that was persisted, can only be called from inside the handlers of anEventSourcedBehavior
protected SignalHandlerBuilder<State>
newSignalHandlerBuilder()
protected abstract ReplyEffect<Event,State>
onCommand(State state, Command command)
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 abstract State
onEvent(State state, Event event)
Implement by applying the event to the current state in order to return a new state.PersistenceId
persistenceId()
Recovery
recovery()
Override to change the strategy for recovery of snapshots and events.RetentionCriteria
retentionCriteria()
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>
signalHandler()
Override to react on general lifecycle signals and persistence specific signals (subtypes ofEventSourcedSignal
).SnapshotAdapter<State>
snapshotAdapter()
Transform the state into another type before giving it to and from the journal.java.lang.String
snapshotPluginId()
Override and define the snapshot store plugin id that this actor should use instead of the default.SnapshotSelectionCriteria
snapshotSelectionCriteria()
Deprecated.override recovery instead.java.util.Optional<java.lang.Integer>
stashCapacity()
Override to define a custom stash capacity per entity.java.util.Set<java.lang.String>
tagsFor(Event event)
Return tags to store for the given event, the tags can then be used in persistence query.java.util.Set<java.lang.String>
tagsFor(State state, Event event)
Return tags to store for the given event and state, the tags can then be used in persistence query.-
Methods inherited from class akka.actor.typed.Behavior
BehaviorDecorators, canonicalize, existsInStack, interpretMessage, interpretSignal, isAlive, isDeferred, isUnhandled, narrow, start, validateAsInitial
-
-
-
-
Constructor Detail
-
EventSourcedOnCommandWithReplyBehavior
public EventSourcedOnCommandWithReplyBehavior(PersistenceId persistenceId, java.util.Optional<BackoffSupervisorStrategy> onPersistFailure)
-
EventSourcedOnCommandWithReplyBehavior
public EventSourcedOnCommandWithReplyBehavior(PersistenceId persistenceId)
- Parameters:
persistenceId
- stable unique identifier for the event sourced behavior
-
EventSourcedOnCommandWithReplyBehavior
public EventSourcedOnCommandWithReplyBehavior(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 Detail
-
Effect
protected final EffectFactories<Event,State> 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
public Behavior<Command> apply(TypedActorContext<Command> context)
INTERNAL API: DeferredBehavior init, not for user extension- Specified by:
apply
in classBehaviorImpl.DeferredBehavior<Command>
-
deleteEventsOnSnapshot
public boolean deleteEventsOnSnapshot()
Can be used to delete events aftershouldSnapshot
.Can be used in combination with
in a way that events are triggered up the the oldest snapshot based onEventSourcedBehavior.retentionCriteria
config.RetentionCriteria.snapshotEvery
- Returns:
true
if events should be deleted aftershouldSnapshot
evaluates to true
-
emptyState
protected abstract State 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
public EventAdapter<Event,?> 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
.
-
journalPluginId
public java.lang.String journalPluginId()
Override and define the journal plugin id that this actor should use instead of the default.
-
lastSequenceNumber
public final long lastSequenceNumber(ActorContext<?> ctx)
The last sequence number that was persisted, can only be called from inside the handlers of anEventSourcedBehavior
-
newSignalHandlerBuilder
protected final SignalHandlerBuilder<State> newSignalHandlerBuilder()
- Returns:
- A new, mutable signal handler builder
-
onCommand
protected abstract ReplyEffect<Event,State> onCommand(State state, Command command)
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.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.
-
onEvent
protected abstract State onEvent(State state, Event event)
Implement by applying the event to the current state in order to return a new state.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.
-
persistenceId
public PersistenceId persistenceId()
-
recovery
public Recovery recovery()
Override to change the strategy for recovery of snapshots and events. By default, snapshots and events are recovered.
-
retentionCriteria
public RetentionCriteria retentionCriteria()
Criteria for retention/deletion of snapshots and events. By default, retention is disabled and snapshots are not saved and deleted automatically.
-
shouldSnapshot
public boolean shouldSnapshot(State state, Event event, long sequenceNr)
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
shouldSnapshot
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
.Events can be deleted if
deleteEventsOnSnapshot
returnstrue
.- Returns:
true
if snapshot should be saved at the givenstate
,event
andsequenceNr
when the event has been successfully persisted
-
signalHandler
protected SignalHandler<State> signalHandler()
Override to react on general lifecycle signals and persistence specific signals (subtypes ofEventSourcedSignal
).Use
EventSourcedBehavior.newSignalHandlerBuilder()
to define the signal handler.
-
snapshotAdapter
public SnapshotAdapter<State> 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.
-
snapshotPluginId
public java.lang.String snapshotPluginId()
Override and define the snapshot store plugin id that this actor should use instead of the default.
-
snapshotSelectionCriteria
public SnapshotSelectionCriteria snapshotSelectionCriteria()
Deprecated.override recovery instead. Since 2.6.5.Override and define the snapshot selection criteria used by this actor instead of the default. By default the most recent snapshot is used, and the remaining state updates are recovered by replaying events from the sequence number up until which the snapshot reached.You may configure the behavior to skip replaying snapshots completely, in which case the recovery will be performed by replaying all events -- which may take a long time.
-
stashCapacity
public java.util.Optional<java.lang.Integer> stashCapacity()
Override to define a custom stash capacity per entity. If not defined, the defaultakka.persistence.typed.stash-capacity
will be used.
-
tagsFor
public java.util.Set<java.lang.String> tagsFor(Event event)
Return tags to store for the given event, the tags can then be used in persistence query.If
tagsFor(Event, State)
is overriden this method is ignored.
-
tagsFor
public java.util.Set<java.lang.String> tagsFor(State state, Event event)
Return tags to store for the given event and state, the tags can then be used in persistence query. The state passed to the tagger allows for toggling a tag with one event but keep all events after it tagged based on a property or the type of the state.
-
-