I
- the type of the aggregate IDsA
- the type of the aggregates managed by this repositorypublic abstract class AggregateRepository<I,A extends Aggregate<I,?,?>> extends Repository<I,A> implements CommandDispatcher, EventProducingRepository
Aggregate
s.Aggregate
Modifier | Constructor and Description |
---|---|
protected |
AggregateRepository()
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
protected AggregateClass<A> |
aggregateClass()
Obtains class information of aggregates managed by this repository.
|
protected AggregateStorage<I> |
aggregateStorage()
Returns the storage assigned to this aggregate.
|
void |
close()
Closes the repository by closing the underlying storage.
|
A |
create(I id)
Create a new entity instance with its default state.
|
protected AggregateStorage<I> |
createStorage()
Creates aggregate storage for the repository.
|
void |
dispatch(CommandEnvelope cmd)
Dispatches the passed command to an aggregate.
|
void |
dispatchEvent(EventEnvelope event)
Dispatches event to one or more aggregates reacting on the event.
|
protected A |
doLoadOrCreate(I id) |
com.google.common.collect.ImmutableSet<EventClass> |
domesticEvents() |
protected void |
doStore(A aggregate) |
EventBus |
eventBus()
Obtains the
EventBus to which the repository posts. |
com.google.common.collect.ImmutableSet<EventClass> |
events() |
com.google.common.collect.ImmutableSet<EventClass> |
externalEvents() |
java.util.Optional<A> |
find(I id)
Loads an aggregate by the passed ID.
|
com.google.common.collect.ImmutableSet<EventClass> |
importableEvents()
Obtains classes of events that can be imported by aggregates of this repository.
|
com.google.common.collect.ImmutableSet<CommandClass> |
messageClasses()
Obtains a set of message classes that can be processed by this dispatcher.
|
com.google.common.collect.ImmutableSet<EventClass> |
outgoingEvents()
Obtains classes of the events produced by entities of this repository.
|
protected A |
play(I id,
io.spine.server.aggregate.AggregateHistory history)
Plays the given Aggregate history for an instance
of
Aggregate with the given ID. |
void |
registerWith(BoundedContext context)
Initializes the repository during its registration with its context.
|
protected void |
setSnapshotTrigger(int snapshotTrigger)
Changes the number of events between making aggregate snapshots to the passed value.
|
protected void |
setupCommandRouting(CommandRouting<I> routing)
A callback for derived classes to customize routing schema for commands.
|
protected void |
setupEventRouting(EventRouting<I> routing)
A callback for derived classes to customize routing schema for events.
|
protected void |
setupImportRouting(EventRouting<I> routing)
A callback for derived classes to customize routing schema for importable events.
|
protected int |
snapshotTrigger()
Returns the number of events until a next
Snapshot is made. |
protected void |
store(A aggregate)
Stores the passed aggregate and commits its uncommitted events.
|
protected AggregateClass<A> |
toModelClass(java.lang.Class<A> cls)
Obtains a model class for the passed entity class value.
|
checkStorage, context, entityClass, entityStateType, eventFilter, hasContext, idClass, index, isOpen, isRegistered, iterator, onRegistered, open, storage, storageAssigned
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
dispatchesCommands
canDispatch
eventFilter, filter, postEvents, postIfCommandRejected
@OverridingMethodsMustInvokeSuper public void registerWith(BoundedContext context)
Verifies that the class of aggregates of this repository subscribes to at least one type of messages.
Registers itself with CommandBus
,
EventBus
, and
ImportBus
of the context for dispatching
messages to its aggregates.
registerWith
in class Repository<I,A extends Aggregate<I,?,?>>
context
- the context of this repositoryjava.lang.IllegalStateException
- if the aggregate class does not handle any messagespublic final EventBus eventBus()
EventProducingRepository
EventBus
to which the repository posts.eventBus
in interface EventProducingRepository
protected void setupCommandRouting(CommandRouting<I> routing)
Default routing returns the value of the first field of a command message.
routing
- the routing schema to customizeprotected void setupEventRouting(EventRouting<I> routing)
Default routing returns the ID of the entity which produced the event. This allows to “link” different kinds of entities by having the same class of IDs. More complex scenarios (e.g. one-to-many relationships) may require custom routing schemas.
routing
- the routing schema to customizeprotected void setupImportRouting(EventRouting<I> routing)
The default routing uses producer ID of the event as the ID of the target aggregate.
This default routing requires that Event
instances
Bus.post(io.spine.core.Signal, io.grpc.stub.StreamObserver) posted}
for import must contain
the ID of the
target aggregate. Not providing a valid aggregate ID would result in
RuntimeException
.
Some aggregates may produce events with the aggregate ID as the first field of an event message. To set the default routing for repositories of such aggregates, please use the code below:
routing.replaceDefault(EventRoute.byFirstMessageField(SomeId.class));
routing
- the routing schema to customize.public A create(I id)
Repository
protected final AggregateClass<A> aggregateClass()
protected AggregateClass<A> toModelClass(java.lang.Class<A> cls)
Repository
toModelClass
in class Repository<I,A extends Aggregate<I,?,?>>
protected final void store(A aggregate)
protected void doStore(A aggregate)
protected AggregateStorage<I> createStorage()
createStorage
in class Repository<I,A extends Aggregate<I,?,?>>
public final com.google.common.collect.ImmutableSet<CommandClass> messageClasses()
MessageDispatcher
messageClasses
in interface MessageDispatcher<CommandClass,CommandEnvelope>
public final void dispatch(CommandEnvelope cmd)
The aggregate ID is obtained from the passed command.
The repository loads the aggregate by this ID, or creates a new aggregate if there is no aggregate with such ID.
dispatch
in interface MessageDispatcher<CommandClass,CommandEnvelope>
cmd
- the command to dispatchpublic com.google.common.collect.ImmutableSet<EventClass> events()
public com.google.common.collect.ImmutableSet<EventClass> domesticEvents()
public com.google.common.collect.ImmutableSet<EventClass> externalEvents()
public com.google.common.collect.ImmutableSet<EventClass> importableEvents()
public com.google.common.collect.ImmutableSet<EventClass> outgoingEvents()
EventProducingRepository
outgoingEvents
in interface EventProducingRepository
public void dispatchEvent(EventEnvelope event)
event
- the event to dispatchprotected int snapshotTrigger()
Snapshot
is made.AggregateRepository.DEFAULT_SNAPSHOT_TRIGGER
protected void setSnapshotTrigger(int snapshotTrigger)
The default value is defined in AggregateRepository.DEFAULT_SNAPSHOT_TRIGGER
.
NOTE: repository read operations are optimized around the current snapshot trigger. Setting the snapshot trigger to a new value may cause read operations to perform sub-optimally, until a new snapshot is created. This doesn't apply to newly created repositories.
snapshotTrigger
- a positive number of the snapshot triggerprotected AggregateStorage<I> aggregateStorage()
java.lang.IllegalStateException
- if the storage is nullprotected A play(I id, io.spine.server.aggregate.AggregateHistory history)
Aggregate
with the given ID.id
- the ID of the Aggregate
to loadhistory
- the state record of the Aggregate
to loadAggregate
@OverridingMethodsMustInvokeSuper public void close()
Repository
The reference to the storage becomes null after this call.