Class JpaEventStorageEngine
- All Implemented Interfaces:
EventStorageEngine
By default, the payload of events is stored as a serialized blob of bytes. Other columns are used to store meta-data that allow quick finding of DomainEvents for a specific aggregate in the correct order.
- Since:
- 3.0
- Author:
- Rene de Waele
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder class to instantiate aJpaEventStorageEngine. -
Field Summary
Fields inherited from class org.axonframework.eventsourcing.eventstore.AbstractEventStorageEngine
upcasterChain -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedInstantiate aJpaEventStorageEnginebased on the fields contained in theJpaEventStorageEngine.Builder. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidappendEvents(List<? extends EventMessage<?>> events, Serializer serializer) Append giveneventsto the backing database.protected static <T> DomainEventMessage<T> asDomainEventMessage(EventMessage<T> event) Converts anEventMessageto aDomainEventMessage.builder()Instantiate a Builder to be able to create aJpaEventStorageEngine.protected ObjectcreateEventEntity(EventMessage<?> eventMessage, Serializer serializer) Returns a Jpa event entity for giveneventMessage.Creates a token that is at the head of an event stream - that tracks all new events.protected ObjectcreateSnapshotEntity(DomainEventMessage<?> snapshot, Serializer serializer) Returns a Jpa snapshot entity for givensnapshotof an aggregate.Creates a token that is at the tail of an event stream - that tracks events from the beginning of time.createTokenAt(Instant dateTime) Creates a token that tracks all events after givendateTime.protected voiddeleteSnapshots(String aggregateIdentifier, long sequenceNumber) Deletes all snapshots from the underlying storage with givenaggregateIdentifier.protected StringReturns the name of the Jpa event entity.protected jakarta.persistence.EntityManagerProvides anEntityManagerinstance for storing and fetching event data.protected List<? extends DomainEventData<?>> fetchDomainEvents(String aggregateIdentifier, long firstSequenceNumber, int batchSize) Returns a batch of events published by an aggregate with givenaggregateIdentifier.fetchEvents(GapAwareTrackingToken token) Returns a batch of event data as object entries in the event storage with a greater than the giventoken.protected List<? extends TrackedEventData<?>> fetchTrackedEvents(TrackingToken lastToken, int batchSize) Returns a batch of serialized event data entries in the event storage that have aTrackingTokengreater than the givenlastToken.lastSequenceNumberFor(String aggregateIdentifier) Returns the last known sequence number for the givenaggregateIdentifier.protected Stream<? extends DomainEventData<?>> readSnapshotData(String aggregateIdentifier) Returns a stream of serialized event entries for givenaggregateIdentifierif the backing database contains a snapshot of the aggregate.voidsetGapCleaningThreshold(int gapCleaningThreshold) Sets the threshold of number of gaps in a token before an attempt to clean gaps up is taken.voidsetGapTimeout(int gapTimeout) Sets the amount of time until a 'gap' in a TrackingToken may be considered timed out.protected StringReturns the name of the Snapshot event entity.protected voidstoreSnapshot(DomainEventMessage<?> snapshot, Serializer serializer) Store the givensnapshotof an Aggregate.Methods inherited from class org.axonframework.eventsourcing.eventstore.BatchingEventStorageEngine
batchSize, fetchForAggregateUntilEmpty, readEventData, readEventDataMethods inherited from class org.axonframework.eventsourcing.eventstore.AbstractEventStorageEngine
appendEvents, getEventSerializer, getSnapshotSerializer, handlePersistenceException, readEvents, readEvents, readSnapshot, storeSnapshotMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.axonframework.eventsourcing.eventstore.EventStorageEngine
appendEvents, readEvents
-
Constructor Details
-
JpaEventStorageEngine
Instantiate aJpaEventStorageEnginebased on the fields contained in theJpaEventStorageEngine.Builder.Will assert that the event and snapshot
Serializer, theEntityManagerProviderandTransactionManagerare notnull, and will throw anAxonConfigurationExceptionif any of them isnull.- Parameters:
builder- theJpaEventStorageEngine.Builderused to instantiate aJpaEventStorageEngineinstance
-
-
Method Details
-
builder
Instantiate a Builder to be able to create aJpaEventStorageEngine.The following configurable fields have defaults:
- The
EventUpcasterdefaults to anNoOpEventUpcaster. - The
PersistenceExceptionResolveris defaulted to aSQLErrorCodesResolver, if theDataSourceis provided - The
snapshotFilterdefaults to aSnapshotFilter.allowAll()instance. - The
batchSizedefaults to an integer of size100. - The
explicitFlushdefaults totrue. - The
maxGapOffsetdefaults to an integer of size10000. - The
lowestGlobalSequencedefaults to a long of size1. - The
gapTimeoutdefaults to an integer of size60000(1 minute). - The
gapCleaningThresholddefaults to an integer of size250.
The event and snapshot
Serializer, theEntityManagerProviderandTransactionManagerare hard requirements and as such should be provided.- Returns:
- a Builder to be able to create a
JpaEventStorageEngine
- The
-
asDomainEventMessage
Converts anEventMessageto aDomainEventMessage. If the message already is aDomainEventMessageit will be returned as is. Otherwise, a newGenericDomainEventMessageis made withnulltype,aggregateIdentifierequal tomessageIdentifierand sequence number of 0L.Doing so allows using the
DomainEventEntryto store both aGenericEventMessageand aGenericDomainEventMessage.- Type Parameters:
T- the type of payload in the message- Parameters:
event- the input event message- Returns:
- the message converted to a domain event message
-
fetchEvents
Returns a batch of event data as object entries in the event storage with a greater than the giventoken. Size of event is decided byBatchingEventStorageEngine.batchSize().- Parameters:
token- Object describing the global index of the last processed event.- Returns:
- A batch of event messages as object stored since the given tracking token.
-
fetchTrackedEvents
protected List<? extends TrackedEventData<?>> fetchTrackedEvents(TrackingToken lastToken, int batchSize) Description copied from class:BatchingEventStorageEngineReturns a batch of serialized event data entries in the event storage that have aTrackingTokengreater than the givenlastToken. Event entries in the stream should be ordered by tracking token. If thelastTokenisnulla stream containing all events should be returned.Only if the returned List is empty the event storage assumes that the backing database holds no further applicable entries.
- Specified by:
fetchTrackedEventsin classBatchingEventStorageEngine- Parameters:
lastToken- Object describing the global index of the last processed event ornullto create a stream of all events in the storebatchSize- The maximum number of events that should be returned- Returns:
- A batch of tracked event messages stored since the given tracking token
-
fetchDomainEvents
protected List<? extends DomainEventData<?>> fetchDomainEvents(String aggregateIdentifier, long firstSequenceNumber, int batchSize) Description copied from class:BatchingEventStorageEngineReturns a batch of events published by an aggregate with givenaggregateIdentifier. The sequence numbers in the returned batch should be ordered by sequence number. The first event in the batch should have a sequence number equal to or larger than givenfirstSequenceNumber. Implementations should make sure the returned batch does not contain gaps between events due to uncommitted storage transactions. If the returned number of entries is smaller than the givenbatchSizeit is assumed that the storage holds no further applicable entries. Implementations for which this is not always the case should overrideBatchingEventStorageEngine.fetchForAggregateUntilEmpty()to returntrueand preferably configure a betterBatchingEventStorageEngine.Builder.finalAggregateBatchPredicate(Predicate)to provide a better heuristic for detecting the last batch in a stream.- Specified by:
fetchDomainEventsin classBatchingEventStorageEngine- Parameters:
aggregateIdentifier- The identifier of the aggregate to open a stream forfirstSequenceNumber- The sequence number of the first excepted event entrybatchSize- The maximum number of events that should be returned- Returns:
- a batch of serialized event entries for the given aggregate
-
readSnapshotData
Description copied from class:AbstractEventStorageEngineReturns a stream of serialized event entries for givenaggregateIdentifierif the backing database contains a snapshot of the aggregate.It is required that specific event storage engines return snapshots in descending order of their sequence number.
- Specified by:
readSnapshotDatain classAbstractEventStorageEngine- Parameters:
aggregateIdentifier- The aggregate identifier to fetch a snapshot for- Returns:
- A stream of serialized snapshots of the aggregate
-
appendEvents
Description copied from class:AbstractEventStorageEngineAppend giveneventsto the backing database. Use the givenserializerto serialize the event's payload and metadata.- Specified by:
appendEventsin classAbstractEventStorageEngine- Parameters:
events- Events to append to the databaseserializer- Serializer used to convert the events to a suitable format for storage
-
storeSnapshot
Description copied from class:AbstractEventStorageEngineStore the givensnapshotof an Aggregate. Implementations may override any existing snapshot of the Aggregate with the given snapshot.- Specified by:
storeSnapshotin classAbstractEventStorageEngine- Parameters:
snapshot- Snapshot Event of the aggregateserializer- Serializer used to convert the snapshot event to a suitable format for storage
-
lastSequenceNumberFor
Description copied from interface:EventStorageEngineReturns the last known sequence number for the givenaggregateIdentifier.While it's recommended to use the sequence numbers from the
DomainEventStream, there are cases where knowing the sequence number is required, without having read the actual events. In such case, this method is a viable alternative.- Parameters:
aggregateIdentifier- The identifier to find the last sequence number for- Returns:
- an optional with the highest sequence number, or an empty optional if the aggregate identifier wasn't found
-
createTailToken
Description copied from interface:EventStorageEngineCreates a token that is at the tail of an event stream - that tracks events from the beginning of time.- Returns:
- a tracking token at the tail of an event stream, if event stream is empty
nullis returned
-
createHeadToken
Description copied from interface:EventStorageEngineCreates a token that is at the head of an event stream - that tracks all new events.- Returns:
- a tracking token at the head of an event stream, if event stream is empty
nullis returned
-
createTokenAt
Description copied from interface:EventStorageEngineCreates a token that tracks all events after givendateTime. If there is an event exactly at the givendateTime, it will be tracked too.- Parameters:
dateTime- The date and time for determining criteria how the tracking token should be created. A tracking token should point to very first event before this date and time.- Returns:
- a tracking token at the given
dateTime, if there aren't events matching this criterianullis returned
-
deleteSnapshots
Deletes all snapshots from the underlying storage with givenaggregateIdentifier.- Parameters:
aggregateIdentifier- the identifier of the aggregate to delete snapshots forsequenceNumber- The sequence number from which value snapshots should be kept
-
createEventEntity
Returns a Jpa event entity for giveneventMessage. Use the givenserializerto serialize the payload and metadata of the event.- Parameters:
eventMessage- the event message to storeserializer- the serializer to serialize the payload and metadata- Returns:
- the Jpa entity to be inserted
-
createSnapshotEntity
Returns a Jpa snapshot entity for givensnapshotof an aggregate. Use the givenserializerto serialize the payload and metadata of the snapshot event.- Parameters:
snapshot- the domain event message containing a snapshot of the aggregateserializer- the serializer to serialize the payload and metadata- Returns:
- the Jpa entity to be inserted
-
domainEventEntryEntityName
Returns the name of the Jpa event entity. Defaults to 'DomainEventEntry'.- Returns:
- the name of the Jpa event entity
-
snapshotEventEntryEntityName
Returns the name of the Snapshot event entity. Defaults to 'SnapshotEventEntry'.- Returns:
- the name of the Jpa snapshot entity
-
entityManager
protected jakarta.persistence.EntityManager entityManager()Provides anEntityManagerinstance for storing and fetching event data.- Returns:
- a provided entity manager
-
setGapTimeout
public void setGapTimeout(int gapTimeout) Sets the amount of time until a 'gap' in a TrackingToken may be considered timed out. This setting will affect the cleaning process of gaps. Gaps that have timed out will be removed from Tracking Tokens to improve performance of reading events. Defaults to 60000 (1 minute).- Parameters:
gapTimeout- The amount of time, in milliseconds until a gap may be considered timed out.
-
setGapCleaningThreshold
public void setGapCleaningThreshold(int gapCleaningThreshold) Sets the threshold of number of gaps in a token before an attempt to clean gaps up is taken. Defaults to 250.- Parameters:
gapCleaningThreshold- The number of gaps before triggering a cleanup.
-