事件调度器
Event Dispatchers
最后更新于
事件发布可以来自 Axon Framework
应用程序的几个位置。一般来说,分为以下两个主要地方:
从聚合(Aggregate
)中调度事件
从常规组件(Non-Aggregate
)中调度事件
This page will describe how to get an event message on the event bus from both locations. For more specifics regarding event publication and storage implementations in Axon Framework, read section.
The or it's are typically the starting point of all event messages. The Event Message simply is the notification that a decision has been made; a successful resolution of handling a command message.
To publish an event from an Aggregate, it is required to do this from the lifecycle of the Aggregate instance. This is mandatory as we want the Aggregate identifier to be tied to the Event message. It is also of the essence that the events originate in order. This is achieved by adding a sequence number to every event from an Aggregate.
The AggregateLifecycle
provides a simple means to achieve the above:
The AggregateLifecycle#apply(Object)
will go through a number of steps:
The current scope of the Aggregate is retrieved.
The last known sequence number of the Aggregate is used to set the sequence number of the event to publish.
The provided Event payload, the Object
, will be wrapped in an EventMessage
.
The EventMessage
will also receive the sequenceNumber
from the previous step, as well as the Aggregate it's identifier.
The Event Message will be published from here on.
The event will first be sent to all the Event Handlers in the Aggregate which are interested.
After the Aggregate itself has handled the event, it will be published on the EventBus
.
MetaData in Aggregate Event Messages
The
AggregateLifecycle
also provides anapply(Object, MetaData)
function. This can be used to attach command-handler specific MetaData.
This is necessary for , to update the Aggregate's state accordingly.
In the vast majority of cases, the will publish events by applying them. However, occasionally, it is necessary to publish an event (possibly from within another component), directly to the Event Gateway: