org.axonframework.eventstore.jpa
Class JpaEventStore

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

public class JpaEventStore
extends Object
implements SnapshotEventStore, EventStoreManagement

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.

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

Since:
0.5
Author:
Allard Buijze

Constructor Summary
JpaEventStore()
          Initialize a JpaEventStore using an XStreamEventSerializer, which serializes events as XML.
JpaEventStore(EventSerializer eventSerializer)
          Initialize a JpaEventStore which serializes events using the given EventSerializer.
 
Method Summary
 void appendEvents(String type, DomainEventStream events)
          Append the events in the given stream to the event store.
 void appendSnapshotEvent(String type, DomainEvent snapshotEvent)
          Append the given snapshotEvent to the snapshot event log for the given type type.
 DomainEventStream readEvents(String type, AggregateIdentifier identifier)
          Read the events of the aggregate identified by the given type and identifier that allow the current aggregate state to be rebuilt.
 DomainEventStream readEventSegment(String type, AggregateIdentifier identifier, long firstSequenceNumber)
          Reads a segment of the events of an aggregate.
 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 setEntityManager(javax.persistence.EntityManager entityManager)
          Sets the EntityManager for this EventStore to use.
 void setPersistenceExceptionResolver(PersistenceExceptionResolver persistenceExceptionResolver)
          Sets the persistenceExceptionResolver that will help detect concurrency exceptions from the backing database.
 void visitEvents(EventVisitor visitor)
          Loads all events available in the event store and calls EventVisitor.doWithEvent(org.axonframework.domain.DomainEvent) 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()
Initialize a JpaEventStore using an XStreamEventSerializer, which serializes events as XML.


JpaEventStore

public JpaEventStore(EventSerializer eventSerializer)
Initialize a JpaEventStore which serializes events using the given EventSerializer.

Parameters:
eventSerializer - The serializer to (de)serialize domain events with.
Method Detail

appendEvents

@Transactional(propagation=MANDATORY)
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,
                                    AggregateIdentifier 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

readEventSegment

public DomainEventStream readEventSegment(String type,
                                          AggregateIdentifier identifier,
                                          long firstSequenceNumber)
Reads a segment of the events of an aggregate. The sequence number of the first event in de the domain event stream is equal to the given firstSequenceNumber, if any. If no events with sequence number equal or greater to the firstSequenceNumber are available, an empty DomainEventStream is returned.

The DomainEventStream returned by this call will never contain any snapshot events.

Note: To return all events after the firstSequenceNumber, use Long.MAX_VALUE as lastSequenceNumber.

Parameters:
type - The type descriptor of the object to retrieve
identifier - The unique aggregate identifier of the events to load
firstSequenceNumber - The sequence number of the first event to return
Returns:
a DomainEventStream containing a segment of past events of an aggregate

appendSnapshotEvent

public void appendSnapshotEvent(String type,
                                DomainEvent snapshotEvent)
Description copied from interface: SnapshotEventStore
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.

Note that the aggregate identifier and sequence number must be set on the DomainEvent. See DomainEvent.DomainEvent(long, org.axonframework.domain.AggregateIdentifier).

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.
See Also:
org.axonframework.domain.DomainEvent#DomainEvent(long, org.axonframework.domain.AggregateIdentifier))

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.DomainEvent) 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

setEntityManager

public void setEntityManager(javax.persistence.EntityManager entityManager)
Sets the EntityManager for this EventStore to use. This EntityManager must be assigned to a persistence context that contains the DomainEventEntry as one of the managed entity types.

Parameters:
entityManager - the EntityManager to use.

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


Copyright © 2011. All Rights Reserved.