Class EventSourcedBehavior<Command,​Event,​State>

    • 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).

        Returns:
        (undocumented)
      • commandHandler

        protected abstract CommandHandler<Command,​Event,​State> commandHandler()
        Implement by handling incoming commands and return an Effect() 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.

        Returns:
        (undocumented)
      • 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".

        Returns:
        (undocumented)
      • 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 than Event.
        Returns:
        (undocumented)
      • eventHandler

        protected abstract EventHandler<State,​Event> 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 or recoveryCompleted blocks.

        Returns:
        (undocumented)
      • journalPluginId

        public java.lang.String journalPluginId()
        Override and define the journal plugin id that this actor should use instead of the default.
        Returns:
        (undocumented)
      • lastSequenceNumber

        public final long lastSequenceNumber​(ActorContext<?> ctx)
        The last sequence number that was persisted, can only be called from inside the handlers of an EventSourcedBehavior
        Parameters:
        ctx - (undocumented)
        Returns:
        (undocumented)
      • newEventHandlerBuilder

        protected final EventHandlerBuilder<State,​Event> newEventHandlerBuilder()
        Returns:
        A new, mutable, event handler builder
      • newSignalHandlerBuilder

        protected final SignalHandlerBuilder<State> newSignalHandlerBuilder()
        Returns:
        A new, mutable signal handler builder
      • 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.
        Returns:
        (undocumented)
      • 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 snapshotWhen will not trigger deletes of old snapshots and events if EventSourcedBehavior.retentionCriteria with RetentionCriteria.snapshotEvery is used together with shouldSnapshot. Such deletes are only triggered by snapshots matching the numberOfEvents in the RetentionCriteria.

        Parameters:
        state - (undocumented)
        event - (undocumented)
        sequenceNr - (undocumented)
        Returns:
        true if snapshot should be saved at the given state, event and sequenceNr when the event has been successfully persisted
      • 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.
        Returns:
        (undocumented)
      • snapshotPluginId

        public java.lang.String snapshotPluginId()
        Override and define the snapshot store plugin id that this actor should use instead of the default.
        Returns:
        (undocumented)
      • snapshotSelectionCriteria

        public SnapshotSelectionCriteria snapshotSelectionCriteria()
        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.

        Returns:
        (undocumented)
      • tagsFor

        public java.util.Set<java.lang.String> tagsFor​(Event event)
        The tagger function should give event tags, which will be used in persistence query
        Parameters:
        event - (undocumented)
        Returns:
        (undocumented)