T
- The type of the aggregate root objectpublic class AnnotatedAggregate<T> extends AggregateLifecycle implements Aggregate<T>, ApplyMore
Aggregate
interface that allows for an aggregate root to be a POJO with annotations on
its Command and Event Handler methods.
This wrapper ensures that aggregate members can use the AggregateLifecycle.apply(Object)
method in a static
context, as long as access to the instance is done via the execute(Consumer)
or invoke(Function)
methods.
AggregateLifecycle.apply(Object)
,
AggregateLifecycle.markDeleted()
Modifier and Type | Field and Description |
---|---|
protected AggregateModel<T> |
inspector |
Modifier | Constructor and Description |
---|---|
protected |
AnnotatedAggregate(AggregateModel<T> inspector,
EventBus eventBus)
Initialize an Aggregate instance for the given
aggregateRoot , described by the given aggregateModel that will publish events to the given eventBus . |
protected |
AnnotatedAggregate(AggregateModel<T> inspector,
EventBus eventBus,
RepositoryProvider repositoryProvider)
Initialize an Aggregate instance for the given
aggregateRoot , described by the given aggregateModel that will publish events to the given eventBus . |
protected |
AnnotatedAggregate(T aggregateRoot,
AggregateModel<T> model,
EventBus eventBus)
Initialize an Aggregate instance for the given
aggregateRoot , described by the given aggregateModel that will publish events to the given eventBus . |
protected |
AnnotatedAggregate(T aggregateRoot,
AggregateModel<T> model,
EventBus eventBus,
RepositoryProvider repositoryProvider)
Initialize an Aggregate instance for the given
aggregateRoot , described by the given aggregateModel that will publish events to the given eventBus . |
Modifier and Type | Method and Description |
---|---|
ApplyMore |
andThen(Runnable runnable)
Execute the given
runnable after applying the previous event. |
ApplyMore |
andThenApply(Supplier<?> payloadOrMessageSupplier)
Apply a subsequent event to the aggregate after applying another event.
|
protected void |
applyMessageOrPayload(Object payloadOrMessage)
Apply a new event message to the aggregate and then publish this message to external systems.
|
protected <P> EventMessage<P> |
createMessage(P payload,
MetaData metaData)
|
protected <P> ApplyMore |
doApply(P payload,
MetaData metaData)
Apply a
DomainEventMessage with given payload and metadata (metadata from
interceptors will be combined with the provided metadata). |
protected <R> Aggregate<R> |
doCreateNew(Class<R> aggregateType,
Callable<R> factoryMethod)
Creates a new aggregate instance.
|
protected void |
doMarkDeleted()
Marks this aggregate as deleted.
|
void |
execute(Consumer<T> invocation)
Execute a method on the underlying aggregate or one of its instances.
|
T |
getAggregateRoot()
Get the annotated aggregate instance.
|
protected boolean |
getIsLive()
Indicates whether this Aggregate instance is 'live'.
|
Object |
handle(Message<?> message)
Handle the given
message on the aggregate root or one of its child entities. |
Object |
identifier()
Retrieve a
Object denoting the identifier of this Aggregate. |
static <T> AnnotatedAggregate<T> |
initialize(Callable<T> aggregateFactory,
AggregateModel<T> aggregateModel,
EventBus eventBus)
Initialize an aggregate created by the given
aggregateFactory which is described in the given aggregateModel . |
static <T> AnnotatedAggregate<T> |
initialize(Callable<T> aggregateFactory,
AggregateModel<T> aggregateModel,
EventBus eventBus,
boolean generateSequences)
Initialize an aggregate created by the given
aggregateFactory which is described in the given aggregateModel . |
static <T> AnnotatedAggregate<T> |
initialize(Callable<T> aggregateFactory,
AggregateModel<T> aggregateModel,
EventBus eventBus,
RepositoryProvider repositoryProvider)
Initialize an aggregate created by the given
aggregateFactory which is described in the given aggregateModel . |
static <T> AnnotatedAggregate<T> |
initialize(Callable<T> aggregateFactory,
AggregateModel<T> aggregateModel,
EventBus eventBus,
RepositoryProvider repositoryProvider,
boolean generateSequences)
Initialize an aggregate created by the given
aggregateFactory which is described in the given aggregateModel . |
static <T> AnnotatedAggregate<T> |
initialize(T aggregateRoot,
AggregateModel<T> aggregateModel,
EventBus eventBus)
Initialize an aggregate with the given
aggregateRoot which is described in the given aggregateModel . |
static <T> AnnotatedAggregate<T> |
initialize(T aggregateRoot,
AggregateModel<T> aggregateModel,
EventBus eventBus,
RepositoryProvider repositoryProvider)
Initialize an aggregate with the given
aggregateRoot which is described in the given aggregateModel . |
void |
initSequence()
Enable sequences on this Aggregate, causing it to emit DomainEventMessages, starting at sequence 0.
|
void |
initSequence(long lastKnownSequenceNumber)
Enable sequences on this Aggregate, causing it to emit DomainEventMessages based on the given
lastKnownSequenceNumber . |
<R> R |
invoke(Function<T,R> invocation)
Invoke a method on the underlying aggregate root or one of its instances.
|
boolean |
isDeleted()
Check if this aggregate has been deleted.
|
Long |
lastSequence()
Returns the last sequence of any event published, or
null if no events have been published yet. |
protected void |
publish(EventMessage<?> msg)
Publish an event to the aggregate root and its entities first and external event handlers (using the given event
bus) later.
|
protected void |
publishOnEventBus(EventMessage<?> msg)
Publish an event to external event handlers using the given event bus.
|
protected void |
registerRoot(Callable<T> aggregateFactory)
Registers the aggregate root created by the given
aggregateFactory with this aggregate. |
Class<? extends T> |
rootType()
Get the class type of the wrapped aggregate root that the Aggregate defers to for command handling.
|
String |
type()
Retrieve a
String denoting the type of this Aggregate. |
Long |
version()
Gets the version of the aggregate.
|
apply, apply, createNew, describeScope, execute, getInstance, getVersion, isLive, markDeleted
describeCurrentScope, endScope, executeWithResult, getCurrentScope, startScope
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
identifierAsString
andThenApplyIf, andThenIf
protected final AggregateModel<T> inspector
protected AnnotatedAggregate(T aggregateRoot, AggregateModel<T> model, EventBus eventBus)
aggregateRoot
, described by the given aggregateModel
that will publish events to the given eventBus
.aggregateRoot
- The aggregate root instancemodel
- The model describing the aggregate structureeventBus
- The Event Bus to publish generated events onprotected AnnotatedAggregate(T aggregateRoot, AggregateModel<T> model, EventBus eventBus, RepositoryProvider repositoryProvider)
aggregateRoot
, described by the given aggregateModel
that will publish events to the given eventBus
.aggregateRoot
- The aggregate root instancemodel
- The model describing the aggregate structureeventBus
- The Event Bus to publish generated events onrepositoryProvider
- Provides repositories for specific aggregate typesprotected AnnotatedAggregate(AggregateModel<T> inspector, EventBus eventBus)
aggregateRoot
, described by the given aggregateModel
that will publish events to the given eventBus
.inspector
- The AggregateModel that describes the aggregateeventBus
- The Event Bus to publish generated events onprotected AnnotatedAggregate(AggregateModel<T> inspector, EventBus eventBus, RepositoryProvider repositoryProvider)
aggregateRoot
, described by the given aggregateModel
that will publish events to the given eventBus
.inspector
- The AggregateModel that describes the aggregateeventBus
- The Event Bus to publish generated events onrepositoryProvider
- Provides repositories for specific aggregate typespublic static <T> AnnotatedAggregate<T> initialize(Callable<T> aggregateFactory, AggregateModel<T> aggregateModel, EventBus eventBus) throws Exception
aggregateFactory
which is described in the given aggregateModel
. The given eventBus
is used to publish events generated by the aggregate.T
- The type of the Aggregate rootaggregateFactory
- The factory to create the aggregate root instance withaggregateModel
- The model describing the aggregate structureeventBus
- The EventBus to publish events onException
- when an error occurs creating the aggregate root instancepublic static <T> AnnotatedAggregate<T> initialize(Callable<T> aggregateFactory, AggregateModel<T> aggregateModel, EventBus eventBus, RepositoryProvider repositoryProvider) throws Exception
aggregateFactory
which is described in the given aggregateModel
. The given eventBus
is used to publish events generated by the aggregate.T
- The type of the Aggregate rootaggregateFactory
- The factory to create the aggregate root instance withaggregateModel
- The model describing the aggregate structureeventBus
- The EventBus to publish events onrepositoryProvider
- Provides repositories for specific aggregate typesException
- when an error occurs creating the aggregate root instancepublic static <T> AnnotatedAggregate<T> initialize(Callable<T> aggregateFactory, AggregateModel<T> aggregateModel, EventBus eventBus, boolean generateSequences) throws Exception
aggregateFactory
which is described in the given aggregateModel
. The given eventBus
is used to publish events generated by the aggregate.T
- The type of the Aggregate rootaggregateFactory
- The factory to create the aggregate root instance withaggregateModel
- The model describing the aggregate structureeventBus
- The EventBus to publish events ongenerateSequences
- Whether to generate sequence numbers on events published from this aggregateException
- when an error occurs creating the aggregate root instancepublic static <T> AnnotatedAggregate<T> initialize(Callable<T> aggregateFactory, AggregateModel<T> aggregateModel, EventBus eventBus, RepositoryProvider repositoryProvider, boolean generateSequences) throws Exception
aggregateFactory
which is described in the given aggregateModel
. The given eventBus
is used to publish events generated by the aggregate.T
- The type of the Aggregate rootaggregateFactory
- The factory to create the aggregate root instance withaggregateModel
- The model describing the aggregate structureeventBus
- The EventBus to publish events onrepositoryProvider
- Provides repositories for specific aggregate typesgenerateSequences
- Whether to generate sequence numbers on events published from this aggregateException
- when an error occurs creating the aggregate root instancepublic static <T> AnnotatedAggregate<T> initialize(T aggregateRoot, AggregateModel<T> aggregateModel, EventBus eventBus)
aggregateRoot
which is described in the given aggregateModel
. The given eventBus
is used to publish events generated by the aggregate.T
- The type of the Aggregate rootaggregateRoot
- The aggregate root instanceaggregateModel
- The model describing the aggregate structureeventBus
- The EventBus to publish events onpublic static <T> AnnotatedAggregate<T> initialize(T aggregateRoot, AggregateModel<T> aggregateModel, EventBus eventBus, RepositoryProvider repositoryProvider)
aggregateRoot
which is described in the given aggregateModel
. The given eventBus
is used to publish events generated by the aggregate.T
- The type of the Aggregate rootaggregateRoot
- The aggregate root instanceaggregateModel
- The model describing the aggregate structureeventBus
- The EventBus to publish events onrepositoryProvider
- Provides repositories for specific aggregate typespublic void initSequence()
public void initSequence(long lastKnownSequenceNumber)
lastKnownSequenceNumber
. Each Event applied will increase the sequence, allowing to trace each event back to the
Aggregate instance that published it, in the order published.lastKnownSequenceNumber
- The sequence number to pass into the next event publishedprotected void registerRoot(Callable<T> aggregateFactory) throws Exception
aggregateFactory
with this aggregate. Applies any
delayed events that have not been applied to the aggregate yet.
This is method is commonly called while an aggregate is being initialized.
aggregateFactory
- the factory to create the aggregate rootException
- if the aggregate factory fails to create the aggregate rootpublic String type()
AggregateLifecycle
String
denoting the type of this Aggregate.public Object identifier()
AggregateLifecycle
Object
denoting the identifier of this Aggregate.identifier
in interface Aggregate<T>
identifier
in class AggregateLifecycle
Object
denoting the identifier of this Aggregatepublic Long version()
AggregateLifecycle
version
in interface Aggregate<T>
version
in class AggregateLifecycle
public Long lastSequence()
null
if no events have been published yet. If
sequences aren't enabled for this Aggregate, the this method will also return null;null
if no events have been published yetprotected boolean getIsLive()
AggregateLifecycle
getIsLive
in class AggregateLifecycle
true
if the aggregate is 'live', false
if the aggregate is initializing state based on
historic eventsprotected <R> Aggregate<R> doCreateNew(Class<R> aggregateType, Callable<R> factoryMethod) throws Exception
AggregateLifecycle
Repository
should be
available to the current aggregate. Repository
of an aggregate to be created is exposed to the current
aggregate via RepositoryProvider
.doCreateNew
in class AggregateLifecycle
R
- type of new aggregate to be createdaggregateType
- type of new aggregate to be createdfactoryMethod
- factory method which creates new aggregateException
- thrown if something goes wrong during instantiation of new aggregatepublic <R> R invoke(Function<T,R> invocation)
Aggregate
Aggregate.execute(Consumer)
to obtain an invocation result, for instance in order to query the aggregate.
Note that the use of this method is not recommended as aggregates are not meant to be queried. Relying on this method is commonly a sign of design smell.
public void execute(Consumer<T> invocation)
Aggregate
Note that the use of this method is not recommended as the wrapped aggregate instance is not meant to be exposed. Relying on this method is commonly a sign of design smell.
public boolean isDeleted()
Aggregate
AggregateDeletedException
.public Class<? extends T> rootType()
Aggregate
protected void doMarkDeleted()
AggregateLifecycle
doMarkDeleted
in class AggregateLifecycle
protected void publish(EventMessage<?> msg)
msg
- the event message to publishprotected void publishOnEventBus(EventMessage<?> msg)
msg
- the event message to publishpublic Object handle(Message<?> message) throws Exception
Aggregate
message
on the aggregate root or one of its child entities.handle
in interface Aggregate<T>
message
- The message to be handled by the aggregatenull
if for example handling a
CommandMessage
yields no resultsException
- in case one is triggered during message processingprotected <P> ApplyMore doApply(P payload, MetaData metaData)
AggregateLifecycle
DomainEventMessage
with given payload and metadata (metadata from
interceptors will be combined with the provided metadata). The event should be applied to the aggregate
immediately and scheduled for publication to other event handlers.
The event should be applied on all entities part of this aggregate. If the event is applied from an event handler
of the aggregate and additional events need to be applied that depends on state changes brought about by the
first event the returned ApplyMore
instance should allow for additional events to be applied after this
event.doApply
in class AggregateLifecycle
payload
- the payload of the event to applymetaData
- any meta-data that must be registered with the EventApplyMore
protected <P> EventMessage<P> createMessage(P payload, MetaData metaData)
P
- the payload typepayload
- payload of the resulting messagemetaData
- metadata of the resulting messagepublic T getAggregateRoot()
invoke(Function)
or execute(Consumer)
instead.public ApplyMore andThenApply(Supplier<?> payloadOrMessageSupplier)
ApplyMore
payloadOrMessageSupplier
is asked to provide the subsequent event the initial event has been fully processed by
the aggregate.
If the given supplier passes an object that is an instance of a Message
the
event is applied with the metadata from the message. If the supplied event is not a Message instance it will be
applied as an event without additional metadata.
andThenApply
in interface ApplyMore
payloadOrMessageSupplier
- The next event message or the payload of the next eventpublic ApplyMore andThen(Runnable runnable)
ApplyMore
runnable
after applying the previous event. This runnable
is guaranteed to be
invoked when the previous event has been fully applied to the aggregate.
The given runnable
must not directly alter any state of the aggregate. Instead, it should only decide
if more events should be applied based on the state of the aggregate after the previous event
protected void applyMessageOrPayload(Object payloadOrMessage)
payloadOrMessage
is an instance of a Message
an event message is applied with the payload and metadata
of the given message, otherwise an event message is applied with given payload and empty metadata.payloadOrMessage
- defines the payload and optionally metadata to apply to the aggregateCopyright © 2010–2024. All rights reserved.