com.lightbend.lagom.scaladsl.persistence
Actions consists of functions to process incoming commands and persisted events.
Actions consists of functions to process incoming commands
and persisted events. Actions
is an immutable class.
The context that is used by command handler function.
The context that is used by command handler function. Events are persisted with the context and replies are sent with the context.
the reply type of the command
A command handler returns a Persist
directive that defines what event or events,
if any, to persist.
A command handler returns a Persist
directive that defines what event or events,
if any, to persist. Use the thenPersist
, thenPersistAll
or done
methods of the context
that is passed to the command handler function to create the Persist
directive.
The context that is used by read-only command handlers.
The context that is used by read-only command handlers. Replies are sent with the context.
the reply type of the command
The name of this entity type.
The name of this entity type. It should be unique among the entity
types of the service. By default it is using the short class name
of the concrete PersistentEntity
class. Subclass may override
to define other type names. It is needed to override and retain
the original name when the class name is changed because this name
is part of the key of the store data (it is part of the persistenceId
of the underlying PersistentActor
).
This method is called to notify the entity that the recovery process is finished.
A
PersistentEntity
has a stable entity identifier, with which it can be accessed from anywhere in the cluster. It is run by an actor and the state is persistent using event sourcing.initialState
andbehavior
are abstract methods that your concrete subclass must implement. The behavior is defined as a set of actions given a state. The actions are functions to process incoming commands and persisted events.The
PersistentEntity
receives commands of typeCommand
that can be validated before persisting state changes as events of typeEvent
. The functions that process incoming commands are registered in theActions
usingonCommand
of theActions
.A command may also be read-only and only perform some side-effect, such as replying to the request. Such command handlers are registered using
onReadOnlyCommand
of theActions
. Replies are sent with thereply
method of the context that is passed to the command handler function.A command handler returns a
Persist
directive that defines what event or events, if any, to persist. Use thethenPersist
,thenPersistAll
ordone
methods of the context that is passed to the command handler function to create thePersist
directive.When an event has been persisted successfully the state of type
State
is updated by applying the event to the current state. The functions for updating the state are registered with theonEvent
method of theActions
. The event handler returns the new state. The state must be immutable, so you return a new instance of the state. Current state is passed as parameter to the event handler. The same event handlers are also used when the entity is started up to recover its state from the stored events.After persisting an event, external side effects can be performed in the
afterPersist
function that can be defined when creating thePersist
directive. A typical side effect is to reply to the request to confirm that it was performed successfully. Replies are sent with thereply
method of the context that is passed to the command handler function.The event handlers are typically only updating the state, but they may also change the behavior of the entity in the sense that new functions for processing commands and events may be defined for a given state. This is useful when implementing finite state machine (FSM) like entities.
When the entity is started the state is recovered by replaying stored events. To reduce this recovery time the entity may start the recovery from a snapshot of the state and then only replaying the events that were stored after the snapshot. Such snapshots are automatically saved after a configured number of persisted events.