Testing order of eventHandlers

Hi,

We have a requirement that 2 projection writers listen to the same event but one EventHandler should definitely be fired before the other. So I added @ProcessingGroup and @Order.

Class 1

`
@ProcessingGroup(“employeeprocessinggroup”)
@Order(2)
@Component
public class EmployeeCorrelationWriter {

private CorrelationRepository repository;

@Autowired
public EmployeeCorrelationWriter(CorrelationRepository repository) {
this.repository = repository;
}

@EventHandler
public void on(EmployeeEnrolledEvent event) {
LOGGER.debug(“Handling event {}”,event);
Correlation correlation = new Correlation(event.employeeId, null, event.employerId, null);
repository.save(correlation);

`

Class 2

`
@ProcessingGroup(“employeeprocessinggroup”)
@Order(1)
@Component
public class EmployeeWriter {

private static final Logger LOGGER = getLogger(EmployeeWriter.class);

// Eventhandlers for enrollment of an employee
@EventHandler
public void on(EmployeeEnrolledEvent evt) {
LOGGER.debug(“Handling {}”, evt.toString());
// more logic
`

Questions:

  1. Is there a way to debug in which sequence axon will run the event handlers?

  2. Is there a way to test this behaviour?

Regards, Norbert

@Allard&Steven: Great course last week :slight_smile:

Hi Norbert,

Thanks for the compliment on the training! :slight_smile:

First off, I’d like to give you a warning. Although it is awesome that you can impose an order on Event Handling Components if they are part of the same processing group, relying on this to much might make the event handling side of your application quite complicated. I’d thus suggest to use this feature sparingly.

Now, to your questions, for which I am assuming you’re on the latest Axon release, 4.0.3:

  1. The AbstractEventProcessor has the processInUnitOfWork() function, which on line 141 calls the EventHandlerInvoker#handle(EventMessage, Segment) function. Entering this handle call will likely end you up in the SimpleEventHandlerInvoker class. This class has a list of EventMessageHandler. The order of this list is the order in which your Event Handling Components will be executed.
  2. Axon does not provide any handles to notify you when it enters which Event Handling Component directly. What you might do for an IT where you would want to check the order, is have a MessageHandlerInterceptor which you’d add to your ‘ordered event processor’. This interceptor could in turn add a resource to the UnitOfWork, which you could verify and update in each Event Handling Component.
    I am just thinking of this on the spot however, I am not super sold on the idea yet…

Hope this gives you some guidance Norbert, feel free to further the discussion with other ideas how to test this.

Cheers,
Steven