When events get triggered by command handler via Aggregate.apply(), does these events first gets consumed by the aggregate event sourced handler and only after the execution completion of the event method handler, is then consumed by the other event handlers. Do we have any such hard order defined ?
No worries, Kartik, all questions are fair if you ask me.
The answer is pretty short, too, so let me break it down for you.
The AggregateLifecycle#apply method will firstly invoke all @EventSourcingHandler annotated methods within the scope of the aggregate with the new event. The event will be published on the event bus only after the command handling method is finished.
Publication on the event bus will, in turn, result in other event handlers being invoked.
No worries Kartik, let met break it down for you in code:
class SampleAggregate {
@CommandHandler
public void handle(SampleCommand command) {
// 1. validate state
// 2. publish event
AggregateLifecycle.apply(new SampleEvent());
// 4. proceed with rest of method
}
@EventSourcingHandler
public void on(SampleEvent event) {
// 3. update state
}
}
So, instead of steps 4 and 5 in your sample, you would first reenter the command handling method if there’s more to execute there.
Only once the entire command handler has finished will Axon’s UnitOfWork commit the event publication transaction (as you described under step 4).
Step 5 is asynchronous from the command handling process, as this is a different thread given the nature of the Streaming Event Processors.