Class EventSourcedAggregate<T>

Type Parameters:
T - The type of the aggregate root object
All Implemented Interfaces:
Aggregate<T>, ApplyMore

public class EventSourcedAggregate<T> extends AnnotatedAggregate<T>
Implementation of an Aggregate that is sourced from events that have been published by the aggregate.
  • Constructor Details

    • EventSourcedAggregate

      protected EventSourcedAggregate(T aggregateRoot, AggregateModel<T> model, EventBus eventBus, SnapshotTrigger snapshotTrigger)
      Initializes an Aggregate instance for the given aggregateRoot, based on the given model, which publishes events to the given eventBus.
      Parameters:
      aggregateRoot - The aggregate root instance
      model - The model describing the aggregate structure
      eventBus - The event store to store generated events in
      snapshotTrigger - The trigger to notify of events and initialization
    • EventSourcedAggregate

      protected EventSourcedAggregate(T aggregateRoot, AggregateModel<T> model, EventBus eventBus, RepositoryProvider repositoryProvider, SnapshotTrigger snapshotTrigger)
      Initializes an Aggregate instance for the given aggregateRoot, based on the given model, which publishes events to the given eventBus.
      Parameters:
      aggregateRoot - The aggregate root instance
      model - The model describing the aggregate structure
      eventBus - The event store to store generated events in
      repositoryProvider - Provides repositories for specific aggregate types
      snapshotTrigger - The trigger to notify of events and initialization
    • EventSourcedAggregate

      protected EventSourcedAggregate(AggregateModel<T> model, EventBus eventBus, SnapshotTrigger snapshotTrigger)
      Creates a new EventSourcedAggregate instance based on the given model, which publishes events to the given eventBus. This aggregate is not assigned a root instance yet.
      Parameters:
      model - The model describing the aggregate structure
      eventBus - The event store to store generated events in
      snapshotTrigger - The trigger to notify of events and initialization
      See Also:
    • EventSourcedAggregate

      protected EventSourcedAggregate(AggregateModel<T> model, EventBus eventBus, RepositoryProvider repositoryProvider, SnapshotTrigger snapshotTrigger)
      Creates a new EventSourcedAggregate instance based on the given model, which publishes events to the given eventBus. This aggregate is not assigned a root instance yet.
      Parameters:
      model - The model describing the aggregate structure
      eventBus - The event store to store generated events in
      repositoryProvider - Provides repositories for specific aggregate types
      snapshotTrigger - The trigger to notify of events and initialization
      See Also:
  • Method Details

    • initialize

      public static <T> EventSourcedAggregate<T> initialize(T aggregateRoot, AggregateModel<T> inspector, EventBus eventBus, SnapshotTrigger snapshotTrigger)
      Initializes an EventSourcedAggregate instance for the given aggregateRoot, based on the given inspector, which publishes events to the given eventBus.
      Type Parameters:
      T - the aggregate root type
      Parameters:
      aggregateRoot - The aggregate root instance
      inspector - The inspector describing the aggregate structure
      eventBus - The event bus to send generated events to
      snapshotTrigger - The trigger to notify of events and initialization
      Returns:
      the initialized EventSourcedAggregate instance
    • initialize

      public static <T> EventSourcedAggregate<T> initialize(T aggregateRoot, AggregateModel<T> inspector, EventBus eventBus, RepositoryProvider repositoryProvider, SnapshotTrigger snapshotTrigger)
      Initializes an EventSourcedAggregate instance for the given aggregateRoot, based on the given inspector, which publishes events to the given eventBus.
      Type Parameters:
      T - the aggregate root type
      Parameters:
      aggregateRoot - The aggregate root instance
      inspector - The inspector describing the aggregate structure
      eventBus - The event bus to send generated events to
      repositoryProvider - Provides repositories for specific aggregate types
      snapshotTrigger - The trigger to notify of events and initialization
      Returns:
      the initialized EventSourcedAggregate instance
    • initialize

      public static <T> EventSourcedAggregate<T> initialize(Callable<T> aggregateFactory, AggregateModel<T> inspector, EventBus eventBus, SnapshotTrigger snapshotTrigger) throws Exception
      Initializes an EventSourcedAggregate instance using the given aggregateFactory, based on the given inspector, which publishes events to the given eventBus and stores events in the given eventStore.
      Type Parameters:
      T - the aggregate root type
      Parameters:
      aggregateFactory - The aggregate root factory
      inspector - The inspector describing the aggregate structure
      eventBus - The event bus to send generated events to
      snapshotTrigger - The trigger to notify of events and initialization
      Returns:
      the initialized EventSourcedAggregate instance
      Throws:
      Exception - if the aggregate cannot be initialized
    • initialize

      public static <T> EventSourcedAggregate<T> initialize(Callable<T> aggregateFactory, AggregateModel<T> inspector, EventBus eventBus, RepositoryProvider repositoryProvider, SnapshotTrigger snapshotTrigger) throws Exception
      Initializes an EventSourcedAggregate instance using the given aggregateFactory, based on the given inspector, which publishes events to the given eventBus and stores events in the given eventStore.
      Type Parameters:
      T - the aggregate root type
      Parameters:
      aggregateFactory - The aggregate root factory
      inspector - The inspector describing the aggregate structure
      eventBus - The event bus to send generated events to
      repositoryProvider - Provides repositories for specific aggregate types
      snapshotTrigger - The trigger to notify of events and initialization
      Returns:
      the initialized EventSourcedAggregate instance
      Throws:
      Exception - if the aggregate cannot be initialized
    • reconstruct

      public static <T> EventSourcedAggregate<T> reconstruct(T aggregateRoot, AggregateModel<T> model, long seqNo, boolean isDeleted, EventBus eventBus, SnapshotTrigger snapshotTrigger)
      Reconstructs an EventSourcedAggregate instance with given aggregateRoot. The aggregate's sequence number should be set to the given seqNo and its deleted flag to the given isDeleted.

      Use this method to initialize an EventSourcedAggregate without having to replay the aggregate from events.

      Type Parameters:
      T - the aggregate root type
      Parameters:
      aggregateRoot - The aggregate root instance
      model - The model describing the aggregate structure
      seqNo - The last event sequence number of the aggregate
      isDeleted - Flag to indicate whether or not the aggregate is deleted
      eventBus - The event bus to send generated events to
      snapshotTrigger - The trigger to notify of events and initialization
      Returns:
      the reconstructed EventSourcedAggregate instance
    • reconstruct

      public static <T> EventSourcedAggregate<T> reconstruct(T aggregateRoot, AggregateModel<T> model, long seqNo, boolean isDeleted, EventBus eventBus, RepositoryProvider repositoryProvider, SnapshotTrigger snapshotTrigger)
      Reconstructs an EventSourcedAggregate instance with given aggregateRoot. The aggregate's sequence number should be set to the given seqNo and its deleted flag to the given isDeleted.

      Use this method to initialize an EventSourcedAggregate without having to replay the aggregate from events.

      Type Parameters:
      T - the aggregate root type
      Parameters:
      aggregateRoot - The aggregate root instance
      model - The model describing the aggregate structure
      seqNo - The last event sequence number of the aggregate
      isDeleted - Flag to indicate whether or not the aggregate is deleted
      eventBus - The event bus to send generated events to
      repositoryProvider - Provides repositories for specific aggregate types
      snapshotTrigger - The trigger to notify of events and initialization
      Returns:
      the reconstructed EventSourcedAggregate instance
    • doApply

      public <P> ApplyMore doApply(P payload, MetaData metaData)
      Description copied from class: AggregateLifecycle
      Apply a DomainEventMessage with given payload and metadata (metadata from interceptors will be combined with the provided metadata). The event should be applied to the aggregate immediately and scheduled for publication to other event handlers.

      The event should be applied on all entities part of this aggregate. If the event is applied from an event handler of the aggregate and additional events need to be applied that depends on state changes brought about by the first event the returned ApplyMore instance should allow for additional events to be applied after this event.

      Overrides:
      doApply in class AnnotatedAggregate<T>
      Parameters:
      payload - the payload of the event to apply
      metaData - any meta-data that must be registered with the Event
      Returns:
      a gizmo to apply additional events after the given event has been processed by the entire aggregate
      See Also:
    • publish

      protected void publish(EventMessage<?> msg)
      Description copied from class: AnnotatedAggregate
      Publish an event to the aggregate root and its entities first and external event handlers (using the given event bus) later.
      Overrides:
      publish in class AnnotatedAggregate<T>
      Parameters:
      msg - the event message to publish
    • publishOnEventBus

      protected void publishOnEventBus(EventMessage<?> msg)
      Description copied from class: AnnotatedAggregate
      Publish an event to external event handlers using the given event bus.
      Overrides:
      publishOnEventBus in class AnnotatedAggregate<T>
      Parameters:
      msg - the event message to publish
    • version

      public Long version()
      Description copied from class: AggregateLifecycle
      Gets the version of the aggregate.
      Specified by:
      version in interface Aggregate<T>
      Overrides:
      version in class AnnotatedAggregate<T>
      Returns:
      the current version of the aggregate
    • initializeState

      public void initializeState(DomainEventStream eventStream)
      Initialize the state of this Event Sourced Aggregate with the events from the given eventStream.
      Parameters:
      eventStream - The Event Stream containing the events to be used to reconstruct this Aggregate's state.
    • getIsLive

      protected boolean getIsLive()
      Description copied from class: AggregateLifecycle
      Indicates whether this Aggregate instance is 'live'. This means events currently applied represent events that are currently happening, as opposed to events representing historic decisions.
      Overrides:
      getIsLive in class AnnotatedAggregate<T>
      Returns:
      true if the aggregate is 'live', false if the aggregate is initializing state based on historic events
    • getSnapshotTrigger

      public SnapshotTrigger getSnapshotTrigger()
      The trigger instance that monitors this aggregate to trigger a snapshot
      Returns:
      the trigger instance assigned to this aggregate instance