Correct way to publish event from within Aggregate without applying it?


I would like to publish a (validation)event from within an aggregate without applying it.
I am aware of the EventTemplate class & registerEvent(metaData, payload) method. I am a bit uncertain of which would be the correct means to achieve this.
Using the EventTemplate would mean I would have to wire it into each @CommandHandler method.

I am unsure of the registerEvent method usage, in the source it is noted that the event is published when the aggregate is saved. Does that mean if I am constructing the AR & validation does not succeed, this event would not be published as technically there is no save taking place?


Upon doing some research in the source I have noticed the CorrelationDataHolder in the EventTemplate.
My need for injecting the EventTemplate was in order to pass MetaData to it from the Command’s MetaData.

Would it be as simple as wiring the EventTemplate where needed & publishing an event? Would the EventTemplate createEventMessage get any correlationData associated with the ThreadLocal & would Command metadata automatically be included in the EventMessage?

Right now I have implemented as below, I would like to publish validation events from within my ruleSpec but have the (correlation)metadata attached to the validation event.

public void handle(MyCommand myCommand, MyBusinessRuleSpec ruleSpec, MetaData metaData){

apply(myCommand, metaData);


injecting the EventTemplate in your aggregate would be one way to do it. If you need to attach metadata from an incoming command to the events that were generated during the processing of that command, simply use the AuditingInterceptor and give it an AuditDataProvider. The latter is an interface that you need to implement yourself. You receive an incoming command message, and need to return the meta data that you want attached to each event processed as part of the command.

Note that this also counts for events applied in an aggregate. When you use an EventBus directly, you need to manually attach meta data.