org.axonframework.eventstore.jdbc
Class JdbcEventStore

java.lang.Object
  extended by org.axonframework.eventstore.jdbc.JdbcEventStore
All Implemented Interfaces:
EventStore, EventStoreManagement, PartialStreamSupport, SnapshotEventStore, UpcasterAware

public class JdbcEventStore
extends Object
implements SnapshotEventStore, EventStoreManagement, UpcasterAware, PartialStreamSupport

An EventStore implementation that uses JPA to store DomainEvents in a database. The actual DomainEvent 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.

This EventStore supports snapshots pruning, which can enabled by configuring a maximum number of snapshots to archive. By default snapshot pruning is configured to archive only snapshot per aggregate.

The serializer used to serialize the events is configurable. By default, the XStreamSerializer is used.

Since:
2.1
Author:
Allard Buijze, Kristian Rosenvold

Constructor Summary
JdbcEventStore(ConnectionProvider connectionProvider)
          Initialize a JdbcEventStore using the default EntryStore and an XStreamSerializer, which serializes events as XML.
JdbcEventStore(DataSource dataSource)
          Initialize a JdbcEventStore using the given eventEntryStore and an XStreamSerializer, which serializes events as XML.
JdbcEventStore(EventEntryStore eventEntryStore)
          Initialize a JdbcEventStore using the given eventEntryStore and an XStreamSerializer, which serializes events as XML.
JdbcEventStore(EventEntryStore eventEntryStore, Serializer serializer)
          Initializes a JdbcEventStore using the given eventEntryStore and serializer.
 
Method Summary
 void appendEvents(String type, DomainEventStream events)
          Append the events in the given stream to the event store.
 void appendSnapshotEvent(String type, DomainEventMessage snapshotEvent)
          Append the given snapshotEvent to the snapshot event log for the given type type.
 CriteriaBuilder newCriteriaBuilder()
          Returns a CriteriaBuilder that allows the construction of criteria for this EventStore implementation
 DomainEventStream readEvents(String type, Object identifier)
          Read the events of the aggregate identified by the given type and identifier that allow the current aggregate state to be rebuilt.
 DomainEventStream readEvents(String type, Object identifier, long firstSequenceNumber)
          Returns a Stream containing events for the aggregate identified by the given type and identifier, starting at the event with the given firstSequenceNumber (included).
 DomainEventStream readEvents(String type, Object identifier, long firstSequenceNumber, long lastSequenceNumber)
          Returns a Stream containing events for the aggregate identified by the given type and identifier, starting at the event with the given firstSequenceNumber (included) up to and including the event with given lastSequenceNumber.
 void setBatchSize(int batchSize)
          Sets the number of events that should be read at each database access.
 void setMaxSnapshotsArchived(int maxSnapshotsArchived)
          Sets the maximum number of snapshots to archive for an aggregate.
 void setPersistenceExceptionResolver(PersistenceExceptionResolver persistenceExceptionResolver)
          Sets the persistenceExceptionResolver that will help detect concurrency exceptions from the backing database.
 void setUpcasterChain(UpcasterChain upcasterChain)
          Sets the UpcasterChain which allow older revisions of serialized objects to be deserialized.
 void visitEvents(Criteria criteria, EventVisitor visitor)
          Loads all events available in the event store that match the given criteria and calls EventVisitor.doWithEvent(org.axonframework.domain.DomainEventMessage) for each event found.
 void visitEvents(EventVisitor visitor)
          Loads all events available in the event store and calls EventVisitor.doWithEvent(org.axonframework.domain.DomainEventMessage) for each event found.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JdbcEventStore

public JdbcEventStore(EventEntryStore eventEntryStore,
                      Serializer serializer)
Initializes a JdbcEventStore using the given eventEntryStore and serializer.

Parameters:
eventEntryStore - The EventEntryStore that stores individual entries in the underlying data source
serializer - The serializer to serialize and deserialize events with

JdbcEventStore

public JdbcEventStore(EventEntryStore eventEntryStore)
Initialize a JdbcEventStore using the given eventEntryStore and an XStreamSerializer, which serializes events as XML.

Parameters:
eventEntryStore - The instance providing persistence logic for Domain Event entries

JdbcEventStore

public JdbcEventStore(ConnectionProvider connectionProvider)
Initialize a JdbcEventStore using the default EntryStore and an XStreamSerializer, which serializes events as XML. The given connectionProvider is used to obtain connections to the underlying data source

Parameters:
connectionProvider - The connection provider to obtain connections from

JdbcEventStore

public JdbcEventStore(DataSource dataSource)
Initialize a JdbcEventStore using the given eventEntryStore and an XStreamSerializer, which serializes events as XML.

Obtains connection from the given dataSource, unless a connection was already obtained in the same Unit of Work, in which case that connection is re-used instead.

Parameters:
dataSource - The DataSource to obtain connections from, when necessary.
Method Detail

appendEvents

public void appendEvents(String type,
                         DomainEventStream events)
Append the events in the given stream to the event store.

Specified by:
appendEvents in interface EventStore
Parameters:
type - The type descriptor of the object to store
events - The event stream containing the events to store

readEvents

public DomainEventStream readEvents(String type,
                                    Object identifier)
Read the events of the aggregate identified by the given type and identifier that allow the current aggregate state to be rebuilt. Implementations may omit or replace events (e.g. by using snapshot events) from the stream for performance purposes.

Specified by:
readEvents in interface EventStore
Parameters:
type - The type descriptor of the object to retrieve
identifier - The unique aggregate identifier of the events to load
Returns:
an event stream containing the events of the aggregate

readEvents

public DomainEventStream readEvents(String type,
                                    Object identifier,
                                    long firstSequenceNumber)
Description copied from interface: PartialStreamSupport
Returns a Stream containing events for the aggregate identified by the given type and identifier, starting at the event with the given firstSequenceNumber (included).

The returned stream will not contain any snapshot events.

Specified by:
readEvents in interface PartialStreamSupport
Parameters:
type - The type identifier of the aggregate
identifier - The identifier of the aggregate
firstSequenceNumber - The sequence number of the first event to find
Returns:
a Stream containing events for the given aggregate, starting at the given first sequence number

readEvents

public DomainEventStream readEvents(String type,
                                    Object identifier,
                                    long firstSequenceNumber,
                                    long lastSequenceNumber)
Description copied from interface: PartialStreamSupport
Returns a Stream containing events for the aggregate identified by the given type and identifier, starting at the event with the given firstSequenceNumber (included) up to and including the event with given lastSequenceNumber. If no event with given lastSequenceNumber exists, the returned stream will simply read until the end of the aggregate's events.

The returned stream will not contain any snapshot events.

Specified by:
readEvents in interface PartialStreamSupport
Parameters:
type - The type identifier of the aggregate
identifier - The identifier of the aggregate
firstSequenceNumber - The sequence number of the first event to find
lastSequenceNumber - The sequence number of the last event in the stream
Returns:
a Stream containing events for the given aggregate, starting at the given first sequence number

appendSnapshotEvent

public void appendSnapshotEvent(String type,
                                DomainEventMessage snapshotEvent)
Append the given snapshotEvent to the snapshot event log for the given type type. The sequence number of the snapshotEvent must be equal to the sequence number of the last regular domain event that is included in the snapshot.

Implementations may choose to prune snapshots upon appending a new snapshot, in order to minimize storage space.

Upon appending a snapshot, this particular EventStore implementation also prunes snapshots which are considered redundant because they fall outside of the range of maximum snapshots to archive.

Specified by:
appendSnapshotEvent in interface SnapshotEventStore
Parameters:
type - The type of aggregate the event belongs to
snapshotEvent - The event summarizing one or more domain events for a specific aggregate.

visitEvents

public void visitEvents(EventVisitor visitor)
Description copied from interface: EventStoreManagement
Loads all events available in the event store and calls EventVisitor.doWithEvent(org.axonframework.domain.DomainEventMessage) for each event found. Events of a single aggregate are guaranteed to be ordered by their sequence number.

Implementations are encouraged, though not required, to supply events in the absolute chronological order.

Processing stops when the visitor throws an exception.

Specified by:
visitEvents in interface EventStoreManagement
Parameters:
visitor - The visitor the receives each loaded event

visitEvents

public void visitEvents(Criteria criteria,
                        EventVisitor visitor)
Description copied from interface: EventStoreManagement
Loads all events available in the event store that match the given criteria and calls EventVisitor.doWithEvent(org.axonframework.domain.DomainEventMessage) for each event found. Events of a single aggregate are guaranteed to be ordered by their sequence number.

Implementations are encouraged, though not required, to supply events in the absolute chronological order.

Processing stops when the visitor throws an exception.

Specified by:
visitEvents in interface EventStoreManagement
Parameters:
criteria - The criteria describing the events to select
visitor - The visitor the receives each loaded event
See Also:
EventStoreManagement.newCriteriaBuilder()

newCriteriaBuilder

public CriteriaBuilder newCriteriaBuilder()
Description copied from interface: EventStoreManagement
Returns a CriteriaBuilder that allows the construction of criteria for this EventStore implementation

Specified by:
newCriteriaBuilder in interface EventStoreManagement
Returns:
a builder to create Criteria for this Event Store.
See Also:
EventStoreManagement.visitEvents(Criteria, org.axonframework.eventstore.EventVisitor)

setPersistenceExceptionResolver

public void setPersistenceExceptionResolver(PersistenceExceptionResolver persistenceExceptionResolver)
Sets the persistenceExceptionResolver that will help detect concurrency exceptions from the backing database.

Parameters:
persistenceExceptionResolver - the persistenceExceptionResolver that will help detect concurrency exceptions

setBatchSize

public void setBatchSize(int batchSize)
Sets 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. Defaults to 100.

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.

Parameters:
batchSize - the number of events to read on each database access. Default to 100.

setUpcasterChain

public void setUpcasterChain(UpcasterChain upcasterChain)
Description copied from interface: UpcasterAware
Sets the UpcasterChain which allow older revisions of serialized objects to be deserialized.

Specified by:
setUpcasterChain in interface UpcasterAware
Parameters:
upcasterChain - the upcaster chain providing the upcasting capabilities

setMaxSnapshotsArchived

public void setMaxSnapshotsArchived(int maxSnapshotsArchived)
Sets the maximum number of snapshots to archive for an aggregate. The EventStore will keep at most this number of snapshots per aggregate.

Defaults to .

Parameters:
maxSnapshotsArchived - The maximum number of snapshots to archive for an aggregate. A value less than 1 disables pruning of snapshots.


Copyright © 2010-2016. All Rights Reserved.