org.axonframework.eventstore.jpa
Class JpaEventStore

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

public class JpaEventStore
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:
0.5
Author:
Allard Buijze

Constructor Summary
JpaEventStore(EntityManagerProvider entityManagerProvider)
          Initialize a JpaEventStore using an XStreamSerializer, which serializes events as XML and the default Event Entry store.
JpaEventStore(EntityManagerProvider entityManagerProvider, EventEntryStore eventEntryStore)
          Initialize a JpaEventStore using the given eventEntryStore and an XStreamSerializer, which serializes events as XML.
JpaEventStore(EntityManagerProvider entityManagerProvider, Serializer serializer)
          Initialize a JpaEventStore which serializes events using the given eventSerializer and stores the events in the database using the default EventEntryStore.
JpaEventStore(EntityManagerProvider entityManagerProvider, Serializer serializer, EventEntryStore eventEntryStore)
          Initialize a JpaEventStore which serializes events using the given eventSerializer and stores the events in the database using the given eventEntryStore.
 
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 setDataSource(DataSource dataSource)
          Registers the data source that allows the EventStore to detect the database type and define the error codes that represent concurrent access failures.
 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

JpaEventStore

public JpaEventStore(EntityManagerProvider entityManagerProvider)
Initialize a JpaEventStore using an XStreamSerializer, which serializes events as XML and the default Event Entry store.

The JPA Persistence context is required to contain two entities: DomainEventEntry and SnapshotEventEntry.

Parameters:
entityManagerProvider - The EntityManagerProvider providing the EntityManager instance for this EventStore

JpaEventStore

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

Parameters:
entityManagerProvider - The EntityManagerProvider providing the EntityManager instance for this EventStore
eventEntryStore - The instance providing persistence logic for Domain Event entries

JpaEventStore

public JpaEventStore(EntityManagerProvider entityManagerProvider,
                     Serializer serializer)
Initialize a JpaEventStore which serializes events using the given eventSerializer and stores the events in the database using the default EventEntryStore.

Note: the SerializedType of Message Meta Data is not stored in the DefaultEventEntryStore. Upon retrieval, it is set to the default value (name = "org.axonframework.domain.MetaData", revision = null). See SerializedMetaData.isSerializedMetaData(org.axonframework.serializer.SerializedObject)

Parameters:
entityManagerProvider - The EntityManagerProvider providing the EntityManager instance for this EventStore
serializer - The serializer to (de)serialize domain events with.

JpaEventStore

public JpaEventStore(EntityManagerProvider entityManagerProvider,
                     Serializer serializer,
                     EventEntryStore eventEntryStore)
Initialize a JpaEventStore which serializes events using the given eventSerializer and stores the events in the database using the given eventEntryStore.

Parameters:
entityManagerProvider - The EntityManagerProvider providing the EntityManager instance for this EventStore
serializer - The serializer to (de)serialize domain events with.
eventEntryStore - The instance providing persistence logic for Domain Event entries
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)

setDataSource

public void setDataSource(DataSource dataSource)
                   throws SQLException
Registers the data source that allows the EventStore to detect the database type and define the error codes that represent concurrent access failures.

Should not be used in combination with setPersistenceExceptionResolver(PersistenceExceptionResolver), but rather as a shorthand alternative for most common database types.

Parameters:
dataSource - A data source providing access to the backing database
Throws:
SQLException - If an error occurs while accessing the dataSource

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.