External Event Handlers not being called or called twice

I am putting together an app with Axon 2.3.2, Spring Context 4.1.1 and Spring Boot 1.1.8.
I followed the getting started tutorial, and all was fine.

Then I refactored my code to pull the command handlers and event handlers into separate classes ie outside the aggregate root (ToDoItem!)
The command handlers are called, but only the event handler I left in the aggrate root is called - my external event handlers are not called.

The only way I can get Axon to call the external handlers is to not do the apply() in the ToDoItem constructor, but instead have the command handler method that created the ToDoItem add it to the repository, then call a method in the ToDoItem, which does the apply(). But then my external event handler method is called twice.

The same happens if I try to use an EventTemplate in the command handler, instead of using the apply() in the aggregate root constructor.

I started by avoiding any spring/axon xml config, instead using annotations throughout, but my annotated event handlers are never called.
I now use only xml, but my annotated external event handlers are either never called, or called twice if I add them to the repository.

Does the apply() only trigger the @EventHandler methods in the aggregate root?

Spinning my wheels here - please help!

Ah. I have made some progress - am I right in thinking that the apply(Event) method in the aggregate root will only result in @EventHandler or @EventSourcingHandler methods in the same aggregate root being called?

And that for an @EventHandler in another class to be called, I need to have my command handler create an EventTemplate and use that to send an event separately?

That seems to work for me, but I would appreciate in someone setting me straight here.



Hi Phil,

when applying an event, only the handlers inside the same aggregate are called. When the aggregate is stored (done automatically when the command completes), the events are published to the event bus. You don’t need to use the EventTemplate there.
Are you using a callback on your command? It’s not unlikely that the command fails for some reason, which could prevent the event from being published on the event bus. If you use the fire and forget method (without callback), the result is logged, but otherwise not visible in the application.



Ah - that explains why I am also not seeing my events persisted to the store. I initially followed the Getting Started page, implementing the ToDoItem example, and I did have persistence working to the file system. Some where along the way, as I refactored the configuration of the app to have my command and event handlers outside of the aggregate, it has stopped working.

I realise now that the instance of the aggregate root that my external command handler creates needs to be added to the repository.
Rather than do that I shall follow the pattern for creation commands whereby the aggregate root constructor is an @CommandHandler.
Now that I have done that, my external event handler works, and persistence is back again.

Thanks Allard!