Imposing an ordering on events handled by @EventHandler

Hi, I’m dealing with a use case where a @ProcessingGroup needs to listen to multiple events that must be processed in a particular order. For instance, these events are coming from Kafka as a underlying source, and I can’t execute the @EventHandler for an event until the @EventHandler of another event has completed. We’ve found a solution, but I’m wondering if Axon provides anything to ease the custom work. Is their a mechanism for deferred processing?

Hi Ryan,

yes, Axon does actually have a solution for this. You can define your own “SequencingPolicy” for each processing group. The SequencingPolicy is basically a function that returns a value based on an incoming event. The default value is the AggregateIdentifier. If the value returned for 2 events is equal, that means Axon will ensure the second event is only handed after the first one completed.

This will even work in situations where the TrackingProcessor has multiple threads across different JVM instances, as long as the SequencingPolicy provides consistent results on all JVMs.

You can use the EventProcessingConfigurer class to define which SequencingPolicy you want to use. The two methods that you’d want to look at are registerDefaultSequencingPolicy and registerSequencingPolicy.

Hope this helps.
Cheers,