Mutable builder for nested Java CommandHandlers where different states should have different command handlers.
Mutable builder for nested Java CommandHandlers where different states should have different command handlers.
CommandHandler per state are added with the match
methods and finally a CommandHandler is created with ByStateCommandHandlerBuilder#build.
Match statements are appended and evaluated in order, the first one to match is used. If no match is found when evaluating the built CommandHandler for a given state a scala.MatchError is thrown.
Instances should not be created by user code but instead accessed through ReplicatedEntity#commandHandlerBuilder.
Not for user extension.
Not for user extension. Instances are created through a builder, see ReplicatedEntity#commandHandlerBuilder
Mutable builder for Java CommandHandlers.
Mutable builder for Java CommandHandlers. Handling of commands are added with the match
methods and finally a CommandHandler is created with CommandHandlerBuilder#build.
Match statements are appended and evaluated in order, the first one to match is used. If no match is found when evaluating the built CommandHandler for a given command a scala.MatchError is thrown.
Instances should not be created by user code but instead accessed through ReplicatedEntity#commandHandlerBuilder.
SAM for reacting on commands
SAM for reacting on commands
Used with CommandHandlerBuilder to setup the behavior of a ReplicatedEntity
A command handler returns a Effect
directive that defines what event or events,
if any, to persist.
A command handler returns a Effect
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 Effect
directive.
Additional side effects can be performed in the callback andThen
Instances of Effect
are available through factories in the respective Java and Scala DSL packages.
Not intended for user extension.
Not intended for user extension.
Not intended for user extension.
Can be created with a EventHandlerBuilder available in ReplicatedEntity#eventHandlerBuilder but can also be provided manually (as a lambda).
Can be created with a EventHandlerBuilder available in ReplicatedEntity#eventHandlerBuilder but can also be provided manually (as a lambda).
A ReplicatedEntity
has a stable entity identifier, with which
it can be accessed from anywhere in the cluster.
A ReplicatedEntity
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
, commandHandler
and eventHandler
are abstract methods that your concrete subclass must implement.
The commandHandler
defines how to process incoming commands, construct one using the builder returned
by commandHandlerBuilder
to use the same handler regardless of state or the
byStateCommandHandlerBuilder
to have different handling depending on state.
Persistent and replicated events are applied to the state with the eventHandler
. Construct an event
handler using the builder returned by eventHandlerBuilder
.
The ReplicatedEntity
receives commands of type Command
that can be validated before
persisting state changes as events of type Event
. The functions that process incoming
commands are registered in the CommandHandler
using onCommand
of the
CommandHandler
.
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 the CommandHandler
. Replies are sent with the sender()
ActorRef
in the context or an
ActorRef
that is passed in the command.
A command handler returns a Effect
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 Effect
directive.
When an event has been persisted successfully the state of type State
is updated by
applying the event to the current state by invoking the eventHandler
.
The eventHandler
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 with the andThen
function of the Effect
directive.
A typical side effect is to reply to the request to confirm that it was performed
successfully. Replies are sent with the sender()
ActorRef
in the context or an
ActorRef
that is passed in the command.
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.
the super type of all commands
the super type of all events
the type of the state
SAM for reacting on signals
SAM for reacting on signals
Used with CommandHandlerBuilder to setup the behavior of a ReplicatedEntity
Singleton object for the effect factories, do not use directly, instead use ReplicatedEntity#Effect