Class AggregateBasedJpaEventStorageEngine

java.lang.Object
org.axonframework.eventsourcing.eventstore.jpa.AggregateBasedJpaEventStorageEngine
All Implemented Interfaces:
DescribableComponent, EventStorageEngine

public class AggregateBasedJpaEventStorageEngine extends Object implements EventStorageEngine
An EventStorageEngine implementation that uses JPA to store and fetch events from an aggregate-based event storage solution.

By default, the payload of events is stored as a converted blob of bytes. Other columns are used to store meta-data that allow quick finding of events for a specific aggregates in the correct order.

Since:
5.0.0
Author:
Mateusz Nowak, Steven van Beelen
  • Constructor Details

  • Method Details

    • appendEvents

      public CompletableFuture<EventStorageEngine.AppendTransaction<?>> appendEvents(AppendCondition condition, @Nullable ProcessingContext processingContext, List<TaggedEventMessage<?>> events)
      Description copied from interface: EventStorageEngine
      Appends a List of events to the underlying storage solution.

      Events will be appended in the order that they are offered in, validating the given condition before being stored. Note that all events should have a unique event identifier. Tags paired with the events will be stored as well.

      Implementations may be able to detect conflicts during the append stage. In such case, the returned completable future will complete exceptionally, indicating such conflict. Other implementations may delay such checks until the EventStorageEngine.AppendTransaction.commit() is called.

      Called during the

      invalid reference
      APPEND_EVENTS
      phase, which follows PREPARE_COMMIT.
      Specified by:
      appendEvents in interface EventStorageEngine
      Parameters:
      condition - The condition describing the transactional requirements for the append transaction
      processingContext - The current ProcessingContext, if any.
      events - The List of events to append to the underlying storage solution.
      Returns:
      A transaction instance that can be committed or rolled back.
    • source

      public MessageStream<EventMessage> source(SourcingCondition condition, @Nullable ProcessingContext context)
      Description copied from interface: EventStorageEngine
      Creates a finite MessageStream of events matching the given condition, carrying along the active ProcessingContext.

      Behaves identically to EventStorageEngine.source(SourcingCondition); the context is passed to decorators (for example snapshot-loading and tracing decorators) that need to correlate the sourcing operation with the surrounding unit of work.

      Specified by:
      source in interface EventStorageEngine
      Parameters:
      condition - the SourcingCondition dictating the stream of events to source
      context - the ProcessingContext active while sourcing; may be null
      Returns:
      a finite MessageStream of events matching the given condition
    • stream

      public MessageStream<EventMessage> stream(StreamingCondition condition)
      Description copied from interface: EventStorageEngine
      Creates an infinite MessageStream of events matching the given condition.

      The condition may dictate the StreamingCondition.position() to start streaming from, as well as define filter criteria for the returned MessageStream.

      Specified by:
      stream in interface EventStorageEngine
      Parameters:
      condition - The StreamingCondition dictating the StreamingCondition.position() to start streaming from, as well as the filter criteria used for the returned MessageStream.
      Returns:
      An infinite MessageStream of events matching the given condition.
    • queryEventsBy

      @Internal protected List<AggregateEventEntry> queryEventsBy(jakarta.persistence.EntityManager entityManager, GapAwareTrackingToken token)
      Queries the next batch of entries that follow the given token, as used by stream(StreamingCondition).

      The result contains every entry with a global index higher than the given token's index, plus any entry whose global index is among the gaps of the token. A null token yields the entries from the start of the event store. Entries are ordered by global index (ascending), and at most batchSize entries are returned.

      Internal API: this operation is exposed only so that custom integrations can adjust how this engine reads from the underlying table, for example to add database-specific hints or locking. It is not part of the public API and may change or be removed in any minor or patch release, without prior notice. Subclasses overriding it do so at their own risk and should re-verify their implementation on every upgrade.

      Overriding implementations should be careful about the following:

      • The result must be ordered by global index, ascending. The caller advances the token over the entries in the order returned, and an out-of-order result leads to incorrect gap tracking, and thus to skipped or duplicated events.
      • The entries at the token's gaps must be included. Omitting them means events that were committed late (out of global index order) are never delivered.
      • The result size is used to detect the head of the event store: fewer than batchSize entries is interpreted as "no more events right now". Returning more or fewer entries than the batch size for any other reason, such as additional filtering in the query, can cause the stream to stall or to poll needlessly. Filtering by the StreamingCondition is done by the caller, not by this operation.
      • The given entityManager belongs to a transaction owned by the caller. Do not commit, roll back or close it.
      • This operation is invoked repeatedly while streaming, so its cost directly affects every active stream and its polling latency.
      Parameters:
      entityManager - the EntityManager to query with, participating in the caller's transaction
      token - the GapAwareTrackingToken to read beyond, or null to read from the start
      Returns:
      the next batch of entries, ordered by global index, containing at most batchSize entries
    • firstToken

      public CompletableFuture<TrackingToken> firstToken()
      Description copied from interface: EventStorageEngine
      Creates a TrackingToken that is at the first position of an event stream.

      In other words, a token that tracks events from the beginning of time.

      Specified by:
      firstToken in interface EventStorageEngine
      Returns:
      A CompletableFuture of a TrackingToken at the first position of an event stream.
    • latestToken

      public CompletableFuture<TrackingToken> latestToken()
      Description copied from interface: EventStorageEngine
      Creates a TrackingToken that is at the latest position of an event stream.

      In other words, a token that tracks all new events from this point forward.

      Specified by:
      latestToken in interface EventStorageEngine
      Returns:
      A CompletableFuture of a TrackingToken at the latest position of an event stream.
    • tokenAt

      public CompletableFuture<TrackingToken> tokenAt(Instant at)
      Description copied from interface: EventStorageEngine
      Creates a TrackingToken that tracks all events after the given at.

      If there is an event exactly at the given at, it will be tracked too.

      Specified by:
      tokenAt in interface EventStorageEngine
      Parameters:
      at - The Instant determining how the TrackingToken should be created. A tracking token should point to very first event before this Instant.
      Returns:
      A CompletableFuture of a TrackingToken at the given at, if there aren't events matching this criteria null is returned
    • describeTo

      public void describeTo(ComponentDescriptor descriptor)
      Description copied from interface: DescribableComponent
      Describe the properties of this DescribableComponent with the given descriptor.

      Components should call the appropriate describeProperty methods on the descriptor to register their properties. The descriptor is responsible for determining how these properties are formatted and structured in the final output.

      Best Practices: As a general rule, all relevant fields of a DescribableComponent implementation should be described in this method. However, developers have discretion to include only the fields that make sense in the context. Not every field may be meaningful for description purposes, especially internal implementation details. Furthermore, components might want to expose different information based on their current state. The final decision on what properties to include lies with the person implementing the describeTo method, who should focus on providing information that is useful for understanding the component's configuration and state.

      Example implementation:

       public void describeTo(ComponentDescriptor descriptor) {
           descriptor.describeProperty("name", this.name);
           descriptor.describeProperty("enabled", this.enabled);
           descriptor.describeProperty("configuration", this.configuration); // A nested component
           descriptor.describeProperty("handlers", this.eventHandlers);      // A collection
       }
       
      Specified by:
      describeTo in interface DescribableComponent
      Parameters:
      descriptor - The component descriptor to describe this DescribableComponentn its properties in.
    • close

      public void close()
      Releases any resources associated with this engine.