Hello guys,
I’m using Axon Framework with Spring, but I came to a problem while implementing a specific use case.
I want to, depending on the business logic, apply an Event and then throw an Exception.
Here is a code example:
@CommandHandler
public void handler(SomeCommand cmd){
if (condition1 == condition2) {
apply(someEvent()); // I want to apply the event
throw new SpecificRuntimeException("exception Error"); // then throw an exception
}
apply(otherEvent());
}
The problem is, when I throw an error, the apply event doesn’t happen.
Why am I using this approach?
Because the SpecificRuntimeException is handled by the ControllerAdvice annotation from Spring, this way, depending on the exception I’m throwing on the command I can specify a different status code for each of them.
A possible solution that I found is, instead of applying the Event on the aggregator command I apply it outside of the aggregator on the ControllerAdvice, here is a piece of code:
long seqNumber = eventStore.lastSequenceNumberFor(instanceKey()).get() + 1L;
eventGateway.publish(new GenericDomainEventMessage<>("someType", instanceKey, seqNumber, new someEvent(ex.getErrorMessages().toString())));
Why am I not convinced with this approach?
- Maybe the eventStore.lastSequenceNumberFor can have thread problems? Also the “+1l”?
- Sending an event outside of the aggregator can be an anti-pattern or “something ugly” to do.
Thank you!
Nelson