Class AnnotatedAggregate<T>
- Type Parameters:
T- The type of the aggregate root object
- Direct Known Subclasses:
EventSourcedAggregate
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.
- Since:
- 3.0
- Author:
- Allard Buijze
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAnnotatedAggregate(AggregateModel<T> inspector, EventBus eventBus) Initialize an Aggregate instance for the givenaggregateRoot, described by the givenaggregateModelthat will publish events to the giveneventBus.protectedAnnotatedAggregate(AggregateModel<T> inspector, EventBus eventBus, RepositoryProvider repositoryProvider) Initialize an Aggregate instance for the givenaggregateRoot, described by the givenaggregateModelthat will publish events to the giveneventBus.protectedAnnotatedAggregate(T aggregateRoot, AggregateModel<T> model, EventBus eventBus) Initialize an Aggregate instance for the givenaggregateRoot, described by the givenaggregateModelthat will publish events to the giveneventBus.protectedAnnotatedAggregate(T aggregateRoot, AggregateModel<T> model, EventBus eventBus, RepositoryProvider repositoryProvider) Initialize an Aggregate instance for the givenaggregateRoot, described by the givenaggregateModelthat will publish events to the giveneventBus. -
Method Summary
Modifier and TypeMethodDescriptionExecute the givenrunnableafter applying the previous event.andThenApply(Supplier<?> payloadOrMessageSupplier) Apply a subsequent event to the aggregate after applying another event.protected voidapplyMessageOrPayload(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> ApplyMoreApply aDomainEventMessagewith 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 voidMarks this aggregate as deleted.voidExecute a method on the underlying aggregate or one of its instances.Get the annotated aggregate instance.protected booleanIndicates whether this Aggregate instance is 'live'.Handle the givenmessageon the aggregate root or one of its child entities.Retrieve aObjectdenoting the identifier of this Aggregate.static <T> AnnotatedAggregate<T> initialize(Callable<T> aggregateFactory, AggregateModel<T> aggregateModel, EventBus eventBus) Initialize an aggregate created by the givenaggregateFactorywhich is described in the givenaggregateModel.static <T> AnnotatedAggregate<T> initialize(Callable<T> aggregateFactory, AggregateModel<T> aggregateModel, EventBus eventBus, boolean generateSequences) Initialize an aggregate created by the givenaggregateFactorywhich is described in the givenaggregateModel.static <T> AnnotatedAggregate<T> initialize(Callable<T> aggregateFactory, AggregateModel<T> aggregateModel, EventBus eventBus, RepositoryProvider repositoryProvider) Initialize an aggregate created by the givenaggregateFactorywhich is described in the givenaggregateModel.static <T> AnnotatedAggregate<T> initialize(Callable<T> aggregateFactory, AggregateModel<T> aggregateModel, EventBus eventBus, RepositoryProvider repositoryProvider, boolean generateSequences) Initialize an aggregate created by the givenaggregateFactorywhich is described in the givenaggregateModel.static <T> AnnotatedAggregate<T> initialize(T aggregateRoot, AggregateModel<T> aggregateModel, EventBus eventBus) Initialize an aggregate with the givenaggregateRootwhich is described in the givenaggregateModel.static <T> AnnotatedAggregate<T> initialize(T aggregateRoot, AggregateModel<T> aggregateModel, EventBus eventBus, RepositoryProvider repositoryProvider) Initialize an aggregate with the givenaggregateRootwhich is described in the givenaggregateModel.voidEnable sequences on this Aggregate, causing it to emit DomainEventMessages, starting at sequence 0.voidinitSequence(long lastKnownSequenceNumber) Enable sequences on this Aggregate, causing it to emit DomainEventMessages based on the givenlastKnownSequenceNumber.<R> RInvoke a method on the underlying aggregate root or one of its instances.booleanCheck if this aggregate has been deleted.Returns the last sequence of any event published, ornullif no events have been published yet.protected voidpublish(EventMessage<?> msg) Publish an event to the aggregate root and its entities first and external event handlers (using the given event bus) later.protected voidpublishOnEventBus(EventMessage<?> msg) Publish an event to external event handlers using the given event bus.protected voidregisterRoot(Callable<T> aggregateFactory) Registers the aggregate root created by the givenaggregateFactorywith this aggregate.rootType()Get the class type of the wrapped aggregate root that the Aggregate defers to for command handling.type()Retrieve aStringdenoting the type of this Aggregate.version()Gets the version of the aggregate.Methods inherited from class org.axonframework.modelling.command.AggregateLifecycle
apply, apply, createNew, describeScope, execute, getInstance, getVersion, isLive, markDeletedMethods inherited from class org.axonframework.messaging.Scope
describeCurrentScope, endScope, executeWithResult, getCurrentScope, startScopeMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.axonframework.modelling.command.Aggregate
identifierAsStringMethods inherited from interface org.axonframework.modelling.command.ApplyMore
andThenApplyIf, andThenIf
-
Field Details
-
inspector
-
-
Constructor Details
-
AnnotatedAggregate
Initialize an Aggregate instance for the givenaggregateRoot, described by the givenaggregateModelthat will publish events to the giveneventBus.- Parameters:
aggregateRoot- The aggregate root instancemodel- The model describing the aggregate structureeventBus- The Event Bus to publish generated events on
-
AnnotatedAggregate
protected AnnotatedAggregate(T aggregateRoot, AggregateModel<T> model, EventBus eventBus, RepositoryProvider repositoryProvider) Initialize an Aggregate instance for the givenaggregateRoot, described by the givenaggregateModelthat will publish events to the giveneventBus.- Parameters:
aggregateRoot- The aggregate root instancemodel- The model describing the aggregate structureeventBus- The Event Bus to publish generated events onrepositoryProvider- Provides repositories for specific aggregate types
-
AnnotatedAggregate
Initialize an Aggregate instance for the givenaggregateRoot, described by the givenaggregateModelthat will publish events to the giveneventBus.- Parameters:
inspector- The AggregateModel that describes the aggregateeventBus- The Event Bus to publish generated events on
-
AnnotatedAggregate
protected AnnotatedAggregate(AggregateModel<T> inspector, EventBus eventBus, RepositoryProvider repositoryProvider) Initialize an Aggregate instance for the givenaggregateRoot, described by the givenaggregateModelthat will publish events to the giveneventBus.- Parameters:
inspector- The AggregateModel that describes the aggregateeventBus- The Event Bus to publish generated events onrepositoryProvider- Provides repositories for specific aggregate types
-
-
Method Details
-
initialize
public static <T> AnnotatedAggregate<T> initialize(Callable<T> aggregateFactory, AggregateModel<T> aggregateModel, EventBus eventBus) throws Exception Initialize an aggregate created by the givenaggregateFactorywhich is described in the givenaggregateModel. The giveneventBusis used to publish events generated by the aggregate.- Type Parameters:
T- The type of the Aggregate root- Parameters:
aggregateFactory- The factory to create the aggregate root instance withaggregateModel- The model describing the aggregate structureeventBus- The EventBus to publish events on- Returns:
- An Aggregate instance, fully initialized
- Throws:
Exception- when an error occurs creating the aggregate root instance
-
initialize
public static <T> AnnotatedAggregate<T> initialize(Callable<T> aggregateFactory, AggregateModel<T> aggregateModel, EventBus eventBus, RepositoryProvider repositoryProvider) throws Exception Initialize an aggregate created by the givenaggregateFactorywhich is described in the givenaggregateModel. The giveneventBusis used to publish events generated by the aggregate.- Type Parameters:
T- The type of the Aggregate root- Parameters:
aggregateFactory- 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 types- Returns:
- An Aggregate instance, fully initialized
- Throws:
Exception- when an error occurs creating the aggregate root instance
-
initialize
public static <T> AnnotatedAggregate<T> initialize(Callable<T> aggregateFactory, AggregateModel<T> aggregateModel, EventBus eventBus, boolean generateSequences) throws Exception Initialize an aggregate created by the givenaggregateFactorywhich is described in the givenaggregateModel. The giveneventBusis used to publish events generated by the aggregate.- Type Parameters:
T- The type of the Aggregate root- Parameters:
aggregateFactory- 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 aggregate- Returns:
- An Aggregate instance, fully initialized
- Throws:
Exception- when an error occurs creating the aggregate root instance
-
initialize
public static <T> AnnotatedAggregate<T> initialize(Callable<T> aggregateFactory, AggregateModel<T> aggregateModel, EventBus eventBus, RepositoryProvider repositoryProvider, boolean generateSequences) throws Exception Initialize an aggregate created by the givenaggregateFactorywhich is described in the givenaggregateModel. The giveneventBusis used to publish events generated by the aggregate.- Type Parameters:
T- The type of the Aggregate root- Parameters:
aggregateFactory- 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 aggregate- Returns:
- An Aggregate instance, fully initialized
- Throws:
Exception- when an error occurs creating the aggregate root instance
-
initialize
public static <T> AnnotatedAggregate<T> initialize(T aggregateRoot, AggregateModel<T> aggregateModel, EventBus eventBus) Initialize an aggregate with the givenaggregateRootwhich is described in the givenaggregateModel. The giveneventBusis used to publish events generated by the aggregate.- Type Parameters:
T- The type of the Aggregate root- Parameters:
aggregateRoot- The aggregate root instanceaggregateModel- The model describing the aggregate structureeventBus- The EventBus to publish events on- Returns:
- An Aggregate instance, fully initialized
-
initialize
public static <T> AnnotatedAggregate<T> initialize(T aggregateRoot, AggregateModel<T> aggregateModel, EventBus eventBus, RepositoryProvider repositoryProvider) Initialize an aggregate with the givenaggregateRootwhich is described in the givenaggregateModel. The giveneventBusis used to publish events generated by the aggregate.- Type Parameters:
T- The type of the Aggregate root- Parameters:
aggregateRoot- The aggregate root instanceaggregateModel- The model describing the aggregate structureeventBus- The EventBus to publish events onrepositoryProvider- Provides repositories for specific aggregate types- Returns:
- An Aggregate instance, fully initialized
-
initSequence
public void initSequence()Enable sequences on this Aggregate, causing it to emit DomainEventMessages, starting at sequence 0. Each Event applied will increase the sequence, allowing to trace each event back to the Aggregate instance that published it, in the order published. -
initSequence
public void initSequence(long lastKnownSequenceNumber) Enable sequences on this Aggregate, causing it to emit DomainEventMessages based on the givenlastKnownSequenceNumber. Each Event applied will increase the sequence, allowing to trace each event back to the Aggregate instance that published it, in the order published.- Parameters:
lastKnownSequenceNumber- The sequence number to pass into the next event published
-
registerRoot
Registers the aggregate root created by the givenaggregateFactorywith 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.
- Parameters:
aggregateFactory- the factory to create the aggregate root- Throws:
Exception- if the aggregate factory fails to create the aggregate root
-
type
Description copied from class:AggregateLifecycleRetrieve aStringdenoting the type of this Aggregate. -
identifier
Description copied from class:AggregateLifecycleRetrieve aObjectdenoting the identifier of this Aggregate.- Specified by:
identifierin interfaceAggregate<T>- Specified by:
identifierin classAggregateLifecycle- Returns:
- a
Objectdenoting the identifier of this Aggregate
-
version
Description copied from class:AggregateLifecycleGets the version of the aggregate.- Specified by:
versionin interfaceAggregate<T>- Specified by:
versionin classAggregateLifecycle- Returns:
- the current version of the aggregate
-
lastSequence
Returns the last sequence of any event published, ornullif no events have been published yet. If sequences aren't enabled for this Aggregate, the this method will also return null;- Returns:
- the last sequence of any event published, or
nullif no events have been published yet
-
getIsLive
protected boolean getIsLive()Description copied from class:AggregateLifecycleIndicates whether this Aggregate instance is 'live'. This means events currently applied represent events that are currently happening, as opposed to events representing historic decisions.- Specified by:
getIsLivein classAggregateLifecycle- Returns:
trueif the aggregate is 'live',falseif the aggregate is initializing state based on historic events
-
doCreateNew
protected <R> Aggregate<R> doCreateNew(Class<R> aggregateType, Callable<R> factoryMethod) throws Exception Description copied from class:AggregateLifecycleCreates a new aggregate instance. In order for new aggregate to be created, aRepositoryshould be available to the current aggregate.Repositoryof an aggregate to be created is exposed to the current aggregate viaRepositoryProvider.- Specified by:
doCreateNewin classAggregateLifecycle- Type Parameters:
R- type of new aggregate to be created- Parameters:
aggregateType- type of new aggregate to be createdfactoryMethod- factory method which creates new aggregate- Returns:
- a new aggregate instance
- Throws:
Exception- thrown if something goes wrong during instantiation of new aggregate
-
invoke
Description copied from interface:AggregateInvoke a method on the underlying aggregate root or one of its instances. Use this overAggregate.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.
-
execute
Description copied from interface:AggregateExecute a method on the underlying aggregate or one of its instances.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.
-
isDeleted
public boolean isDeleted()Description copied from interface:AggregateCheck if this aggregate has been deleted. This is checked by aggregate repositories when an aggregate is loaded. In case the repository is asked to load a deleted aggregate the repository will refuse by throwing anAggregateDeletedException. -
rootType
Description copied from interface:AggregateGet the class type of the wrapped aggregate root that the Aggregate defers to for command handling. -
doMarkDeleted
protected void doMarkDeleted()Description copied from class:AggregateLifecycleMarks this aggregate as deleted. Implementations may react differently to aggregates marked for deletion. Typically, Event Sourced Repositories will ignore the marking and expect deletion to be provided as part of Event information.- Specified by:
doMarkDeletedin classAggregateLifecycle
-
publish
Publish an event to the aggregate root and its entities first and external event handlers (using the given event bus) later.- Parameters:
msg- the event message to publish
-
publishOnEventBus
Publish an event to external event handlers using the given event bus.- Parameters:
msg- the event message to publish
-
handle
Description copied from interface:AggregateHandle the givenmessageon the aggregate root or one of its child entities.- Specified by:
handlein interfaceAggregate<T>- Parameters:
message- The message to be handled by the aggregate- Returns:
- The result of message handling. Might returns
nullif for example handling aCommandMessageyields no results - Throws:
Exception- in case one is triggered during message processing
-
doApply
Description copied from class:AggregateLifecycleApply aDomainEventMessagewith 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 returnedApplyMoreinstance should allow for additional events to be applied after this event.- Specified by:
doApplyin classAggregateLifecycle- Parameters:
payload- the payload of the event to applymetaData- any meta-data that must be registered with the Event- Returns:
- a gizmo to apply additional events after the given event has been processed by the entire aggregate
- See Also:
-
createMessage
- Type Parameters:
P- the payload type- Parameters:
payload- payload of the resulting messagemetaData- metadata of the resulting message- Returns:
- the resulting message
-
getAggregateRoot
Get the annotated aggregate instance. Note that this method should probably never be used in normal use. If you need to operate on the aggregate useinvoke(Function)orexecute(Consumer)instead.- Returns:
- the aggregate instance
-
andThenApply
Description copied from interface:ApplyMoreApply a subsequent event to the aggregate after applying another event. When the givenpayloadOrMessageSupplieris 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
Messagethe 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.- Specified by:
andThenApplyin interfaceApplyMore- Parameters:
payloadOrMessageSupplier- The next event message or the payload of the next event- Returns:
- an instance of ApplyMore to apply any subsequent events
-
andThen
Description copied from interface:ApplyMoreExecute the givenrunnableafter applying the previous event. Thisrunnableis guaranteed to be invoked when the previous event has been fully applied to the aggregate.The given
runnablemust 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 -
applyMessageOrPayload
Apply a new event message to the aggregate and then publish this message to external systems. If the givenpayloadOrMessageis an instance of aMessagean 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.- Parameters:
payloadOrMessage- defines the payload and optionally metadata to apply to the aggregate
-