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.
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(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 . |
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 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(CommandMessage<?> msg)
Handle the given
commandMessage on the aggregate root or one of its child entities. |
Object |
identifier()
Get the unique 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(T aggregateRoot,
AggregateModel<T> aggregateModel,
EventBus eventBus)
Initialize an aggregate with the given
aggregateRoot which is described in the given
aggregateModel . |
<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.
|
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()
Get the String representation of the aggregate's type.
|
Long |
version()
Get the aggregate's version.
|
apply, apply, execute, executeWithResult, getInstance, isLive, markDeleted, registerAsCurrent, registerWithUnitOfWork
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
identifierAsString
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(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 onpublic 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(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 onprotected 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()
Aggregate
Aggregate.rootType()
unless configured otherwise.public Object identifier()
Aggregate
identifier
in interface Aggregate<T>
public Long version()
Aggregate
protected boolean getIsLive()
AggregateLifecycle
getIsLive
in class AggregateLifecycle
true
if the aggregate is 'live', false
if the aggregate is initializing state based on
historic eventspublic <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(CommandMessage<?> msg) throws Exception
Aggregate
commandMessage
on the aggregate root or one of its child entities.protected <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–2017. All rights reserved.