public class JpaEventStorageEngine extends BatchingEventStorageEngine
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.
Constructor and Description |
---|
JpaEventStorageEngine(EntityManagerProvider entityManagerProvider,
TransactionManager transactionManager)
Initializes an EventStorageEngine that uses JPA to store and load events.
|
JpaEventStorageEngine(Serializer serializer,
EventUpcaster upcasterChain,
DataSource dataSource,
EntityManagerProvider entityManagerProvider,
TransactionManager transactionManager)
Initializes an EventStorageEngine that uses JPA to store and load events.
|
JpaEventStorageEngine(Serializer snapshotSerializer,
EventUpcaster upcasterChain,
DataSource dataSource,
Serializer eventSerializer,
EntityManagerProvider entityManagerProvider,
TransactionManager transactionManager)
Initializes an EventStorageEngine that uses JPA to store and load events.
|
JpaEventStorageEngine(Serializer serializer,
EventUpcaster upcasterChain,
PersistenceExceptionResolver persistenceExceptionResolver,
EntityManagerProvider entityManagerProvider,
TransactionManager transactionManager)
Initializes an EventStorageEngine that uses JPA to store and load events.
|
JpaEventStorageEngine(Serializer snapshotSerializer,
EventUpcaster upcasterChain,
PersistenceExceptionResolver persistenceExceptionResolver,
Serializer eventSerializer,
EntityManagerProvider entityManagerProvider,
TransactionManager transactionManager)
Initializes an EventStorageEngine that uses JPA to store and load events.
|
JpaEventStorageEngine(Serializer snapshotSerializer,
EventUpcaster upcasterChain,
PersistenceExceptionResolver persistenceExceptionResolver,
Serializer eventSerializer,
Integer batchSize,
EntityManagerProvider entityManagerProvider,
TransactionManager transactionManager,
Long lowestGlobalSequence,
Integer maxGapOffset,
boolean explicitFlush)
Initializes an EventStorageEngine that uses JPA to store and load events.
|
Modifier and Type | Method and Description |
---|---|
protected void |
appendEvents(List<? extends EventMessage<?>> events,
Serializer serializer)
Append given
events to the backing database. |
protected Object |
createEventEntity(EventMessage<?> eventMessage,
Serializer serializer)
Returns a Jpa event entity for given
eventMessage . |
TrackingToken |
createHeadToken()
Creates a token that is at the head of an event stream - that tracks all new events.
|
protected Object |
createSnapshotEntity(DomainEventMessage<?> snapshot,
Serializer serializer)
Returns a Jpa snapshot entity for given
snapshot of an aggregate. |
TrackingToken |
createTailToken()
Creates a token that is at the tail of an event stream - that tracks events from the beginning of time.
|
TrackingToken |
createTokenAt(Instant dateTime)
Creates a token that tracks all events after given
dateTime . |
protected void |
deleteSnapshots(String aggregateIdentifier,
long sequenceNumber)
Deletes all snapshots from the underlying storage with given
aggregateIdentifier . |
protected String |
domainEventEntryEntityName()
Returns the name of the Jpa event entity.
|
protected javax.persistence.EntityManager |
entityManager()
Provides an
EntityManager instance 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 given
aggregateIdentifier . |
protected List<? extends TrackedEventData<?>> |
fetchTrackedEvents(TrackingToken lastToken,
int batchSize)
Returns a batch of serialized event data entries in the event storage that have a
TrackingToken greater
than the given lastToken . |
Optional<Long> |
lastSequenceNumberFor(String aggregateIdentifier)
Returns the last known sequence number for the given
aggregateIdentifier . |
protected Optional<? extends DomainEventData<?>> |
readSnapshotData(String aggregateIdentifier)
Returns an optional serialized event entry for given
aggregateIdentifier if the backing database
contains a snapshot of the aggregate. |
void |
setGapCleaningThreshold(int gapCleaningThreshold)
Sets the threshold of number of gaps in a token before an attempt to clean gaps up is taken.
|
void |
setGapTimeout(int gapTimeout)
Sets the amount of time until a 'gap' in a TrackingToken may be considered timed out.
|
protected String |
snapshotEventEntryEntityName()
Returns the name of the Snaphot event entity.
|
protected void |
storeSnapshot(DomainEventMessage<?> snapshot,
Serializer serializer)
Store the given
snapshot of an Aggregate. |
batchSize, readEventData, readEventData
appendEvents, getEventSerializer, getSerializer, handlePersistenceException, readEvents, readEvents, readSnapshot, storeSnapshot
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
appendEvents, readEvents
public JpaEventStorageEngine(EntityManagerProvider entityManagerProvider, TransactionManager transactionManager)
XStreamSerializer
.
Events are read in batches of 100. No upcasting is performed after the events have been fetched.
entityManagerProvider
- Provider for the EntityManager
used by this EventStorageEngine.transactionManager
- The instance managing transactions around fetching event data. Required by certain
databases for reading blob data.public JpaEventStorageEngine(Serializer serializer, EventUpcaster upcasterChain, DataSource dataSource, EntityManagerProvider entityManagerProvider, TransactionManager transactionManager) throws SQLException
Serializer
is used for both snapshots and events.serializer
- Used to serialize and deserialize event payload and metadata, and snapshots.upcasterChain
- Allows older revisions of serialized objects to be deserialized.dataSource
- Allows the EventStore to detect the database type and define the error codes that
represent concurrent access failures for most database types.entityManagerProvider
- Provider for the EntityManager
used by this EventStorageEngine.transactionManager
- The instance managing transactions around fetching event data. Required by certain
databases for reading blob data.SQLException
- If the database product name can not be determined from the given dataSource
public JpaEventStorageEngine(Serializer snapshotSerializer, EventUpcaster upcasterChain, DataSource dataSource, Serializer eventSerializer, EntityManagerProvider entityManagerProvider, TransactionManager transactionManager) throws SQLException
snapshotSerializer
- Used to serialize and deserialize snapshots.upcasterChain
- Allows older revisions of serialized objects to be deserialized.dataSource
- Allows the EventStore to detect the database type and define the error codes that
represent concurrent access failures for most database types.eventSerializer
- Used to serialize and deserialize event payload and metadata.entityManagerProvider
- Provider for the EntityManager
used by this EventStorageEngine.transactionManager
- The instance managing transactions around fetching event data. Required by certain
databases for reading blob data.SQLException
- If the database product name can not be determined from the given dataSource
public JpaEventStorageEngine(Serializer serializer, EventUpcaster upcasterChain, PersistenceExceptionResolver persistenceExceptionResolver, EntityManagerProvider entityManagerProvider, TransactionManager transactionManager)
Serializer
is used for both snapshots and events.serializer
- Used to serialize and deserialize event payload and metadata, and snapshots.upcasterChain
- Allows older revisions of serialized objects to be deserialized.persistenceExceptionResolver
- Detects concurrency exceptions from the backing database. If null
persistence exceptions are not explicitly resolved.
that represent concurrent access failures for most database types.entityManagerProvider
- Provider for the EntityManager
used by this EventStorageEngine.transactionManager
- The instance managing transactions around fetching event data. Required by
certain databases for reading blob data.public JpaEventStorageEngine(Serializer snapshotSerializer, EventUpcaster upcasterChain, PersistenceExceptionResolver persistenceExceptionResolver, Serializer eventSerializer, EntityManagerProvider entityManagerProvider, TransactionManager transactionManager)
snapshotSerializer
- Used to serialize and deserialize snapshots.upcasterChain
- Allows older revisions of serialized objects to be deserialized.persistenceExceptionResolver
- Detects concurrency exceptions from the backing database. If null
persistence exceptions are not explicitly resolved.
that represent concurrent access failures for most database types.eventSerializer
- Used to serialize and deserialize event payload and metadata.entityManagerProvider
- Provider for the EntityManager
used by this EventStorageEngine.transactionManager
- The instance managing transactions around fetching event data. Required by
certain databases for reading blob data.public JpaEventStorageEngine(Serializer snapshotSerializer, EventUpcaster upcasterChain, PersistenceExceptionResolver persistenceExceptionResolver, Serializer eventSerializer, Integer batchSize, EntityManagerProvider entityManagerProvider, TransactionManager transactionManager, Long lowestGlobalSequence, Integer maxGapOffset, boolean explicitFlush)
snapshotSerializer
- Used to serialize and deserialize snapshots.upcasterChain
- Allows older revisions of serialized objects to be deserialized.persistenceExceptionResolver
- Detects concurrency exceptions from the backing database. If null
persistence exceptions are not explicitly resolved.eventSerializer
- Used to serialize and deserialize event payload and metadata.batchSize
- The number of events that should be read at each database access. When more
than this number of events must be read to rebuild an aggregate's state, the
events are read in batches of this size. Tip: if you use a snapshotter, make
sure to choose snapshot trigger and batch size such that a single batch will
generally retrieve all events required to rebuild an aggregate's state.entityManagerProvider
- Provider for the EntityManager
used by this EventStorageEngine.maxGapOffset
- The maximum distance in sequence numbers between a missing event and the
event with the highest known index. If the gap is bigger it is assumed that
the missing event will not be committed to the store anymore. This event
storage engine will no longer look for those events the next time a batch is
fetched.explicitFlush
- Whether to explicitly call EntityManager.flush()
after inserting the
Events published in this Unit of Work. If false
, this instance relies
on the transaction manager to flush data. Note that the
persistenceExceptionResolver
may not be able to translate exceptions
anymore. false
Should only be used to optimize performance for batch
operations. In other cases, true
is recommended.transactionManager
- The instance managing transactions around fetching event data. Required by
certain
databases for reading blob data.lowestGlobalSequence
- The first expected auto generated sequence number. For most data stores this
is 1 unless the table has contained entries before.protected List<? extends TrackedEventData<?>> fetchTrackedEvents(TrackingToken lastToken, int batchSize)
BatchingEventStorageEngine
TrackingToken
greater
than the given lastToken
. Event entries in the stream should be ordered by tracking token. If the lastToken
is null
a 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.
fetchTrackedEvents
in class BatchingEventStorageEngine
lastToken
- Object describing the global index of the last processed event or null
to create a
stream of all events in the storebatchSize
- The maximum number of events that should be returnedprotected List<? extends DomainEventData<?>> fetchDomainEvents(String aggregateIdentifier, long firstSequenceNumber, int batchSize)
BatchingEventStorageEngine
aggregateIdentifier
.
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 given firstSequenceNumber
. 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 given batchSize
it is assumed that the storage
holds no further applicable entries.fetchDomainEvents
in class BatchingEventStorageEngine
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 returnedprotected Optional<? extends DomainEventData<?>> readSnapshotData(String aggregateIdentifier)
AbstractEventStorageEngine
aggregateIdentifier
if the backing database
contains a snapshot of the aggregate.readSnapshotData
in class AbstractEventStorageEngine
aggregateIdentifier
- The aggregate identifier to fetch a snapshot forprotected void appendEvents(List<? extends EventMessage<?>> events, Serializer serializer)
AbstractEventStorageEngine
events
to the backing database. Use the given serializer
to serialize the event's
payload and metadata.appendEvents
in class AbstractEventStorageEngine
events
- Events to append to the databaseserializer
- Serializer used to convert the events to a suitable format for storageprotected void storeSnapshot(DomainEventMessage<?> snapshot, Serializer serializer)
AbstractEventStorageEngine
snapshot
of an Aggregate. Implementations may override any existing snapshot of the
Aggregate with the given snapshot.storeSnapshot
in class AbstractEventStorageEngine
snapshot
- Snapshot Event of the aggregateserializer
- Serializer used to convert the snapshot event to a suitable format for storagepublic Optional<Long> lastSequenceNumberFor(String aggregateIdentifier)
EventStorageEngine
aggregateIdentifier
.
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.
aggregateIdentifier
- The identifier to find the last sequence number forpublic TrackingToken createTailToken()
EventStorageEngine
null
is returnedpublic TrackingToken createHeadToken()
EventStorageEngine
null
is returnedpublic TrackingToken createTokenAt(Instant dateTime)
EventStorageEngine
dateTime
. If there is an event exactly at the given
dateTime
, it will be tracked too.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.dateTime
, if there aren't events matching this criteria null
is returnedprotected void deleteSnapshots(String aggregateIdentifier, long sequenceNumber)
aggregateIdentifier
.aggregateIdentifier
- the identifier of the aggregate to delete snapshots forsequenceNumber
- The sequence number from which value snapshots should be keptprotected Object createEventEntity(EventMessage<?> eventMessage, Serializer serializer)
eventMessage
. Use the given serializer
to serialize the
payload and metadata of the event.eventMessage
- the event message to storeserializer
- the serializer to serialize the payload and metadataprotected Object createSnapshotEntity(DomainEventMessage<?> snapshot, Serializer serializer)
snapshot
of an aggregate. Use the given serializer
to
serialize the payload and metadata of the snapshot event.snapshot
- the domain event message containing a snapshot of the aggregateserializer
- the serializer to serialize the payload and metadataprotected String domainEventEntryEntityName()
protected String snapshotEventEntryEntityName()
protected javax.persistence.EntityManager entityManager()
EntityManager
instance for storing and fetching event data.public void setGapTimeout(int gapTimeout)
gapTimeout
- The amount of time, in milliseconds until a gap may be considered timed out.public void setGapCleaningThreshold(int gapCleaningThreshold)
gapCleaningThreshold
- The number of gaps before triggering a cleanup.Copyright © 2010–2018. All rights reserved.