GiftCardas the Aggregate (Root). By default, Axon will configure your Aggregate as an 'Event Sourced' Aggregate (as described here). Henceforth our basic
GiftCardAggregate structure will focus on the Event Sourcing approach:
@AggregateIdentifieris the external reference point to into the
GiftCardAggregate. This field is a hard requirement, as without it Axon will not know to which Aggregate a given Command is targeted. Note that this annotation can be placed on a field and a method.
@CommandHandlerannotated constructor, or differently put the 'command handling constructor'.
@CommandHandlerannotated functions are the place where you would put your decision-making/business logic.
AggregateLifecycle#apply(Object...)is what is used when an Event Message should be published.
Objects will be published as
EventMessages within the scope of the Aggregate they are applied in.
@EventSourcingHandleris what tells the framework that the annotated function should be called when the Aggregate is 'sourced from its events'.
@EventSourcingHandlerof the very first Event published by the aggregate.
@EventSourcingHandlerannotated functions are resolved using specific rules.
@EventHandlerannotated methods, and are thoroughly explained in Annotated Event Handler.
Modifiers for Message Handling functionsEvent Handler methods may be private, as long as the security settings of the JVM allow the Axon Framework to change the accessibility of the method. This allows you to clearly separate the public API of your Aggregate, which exposes the methods that generate events, from the internal logic, which processes the events.Most IDE's have an option to ignore "unused private method" warnings for methods with a specific annotation. Alternatively, you can add an
@SuppressWarnings("UnusedDeclaration")annotation to the method to make sure you do not accidentally delete an event handler method.
AggregateLifecycleclass in Axon provides a couple of static functions:
apply(Object, MetaData): The
AggregateLifecycle#applywill publish an Event message on an
EventBussuch that it is known to have originated from the Aggregate executing the operation.
Objector both the Event and some specific MetaData.
createNew(Class, Callable): Instantiate a new Aggregate as a result of handling a Command.
isLive(): Check to verify whether the Aggregate is in a 'live' state.
AggregateLifecycle.isLive()call would return
isLive()method, you can perform activity that should only be done when handling newly generated events.
markDeleted(): Will mark the Aggregate instance calling the function as being 'deleted'.
@EventSourcingHandlerannotated function to ensure that being marked deleted is part of that Aggregate's state.