Non Aggregate @EventHandler methods are not getting called.

Hi All,

I am facing strange issue, i have same @EventHandler method with same event class in Aggregate and in non Aggregate class, only aggregate @EventHandler methods are called.I assume both methods should be called, one in Aggregate and in non Aggregate(EventListener) class.

I have configured properly as per the ToDoItem example, created bean in spring config file and also added axon:annotation-config/ <context:annotation-config /> and even i tried adding AnnotationEventListenerBeanPostProcessor bean also.

Am i missing any configurations. I am completely blocked after the initial aggregate based conditions i need to publish other commands and events. Please help me.
I have tested with JUnit test cases with Axon 2.0.2 and 2.0.4 no luck.

Have few question for clarity.

  • Can we inject or @Autowire beans in Aggregate class.
  • I am sure at least one event handler method must present in Aggregate class for replay before applying new event in the event store.
  • As a best practice where should i add my business logic either in command handler or EventHandler or EventListener.

Thanks for the help in Advance.

Hi,

did you add the external @EventHandler as a bean in your spring context?

If you want to inject resources in an aggregate, you can either just add them to parameters to you @CommandHander or @EventHandler methods. Axon will automatically inject a Spring bean.

Aggregates don’t need to have an event handler in order to apply a new event. You only need to have a handler for an event that updates state within the aggregate.

The business logic question is too generic to answer. Command handlers make decisions, events handlers update state. This could be either internal aggregate state, but also state of a query database.

Hope this helps.
Cheers,

Allard

Hi Allard,

Thanks for quick reply

By mistake i have replied to author instead of reply to post.

I have created external @EventHandler as bean in spring context file, i even rechecked it again no luck, and i also tried deploying the web application in tomcat also no luck

I am trying with test cases, even i tried the ToDoItem sample test cases, there also external @EventHandler methods are not called. Do we need to register the external @EventHandler methods in test cases, If yes, how to register them.

I tried loading the spring context file with in the public static void main method class and there both external and aggregate methods are called, i am surprised how come its working in main method class and not in test case and tomcat, i used same spring context.

Thanks,

Hi,

with test case, do you mean the Axon test fixtures? They’re not meant to invoke external handlers. They just test the command handling part of the model.

If it works when using a psv main method, but not using Tomcat, the problem must be in the Spring configuration. Do you use an application context as well as a servlet context? If so,in which is your event handler bean configured?

Cheers,

Allard

Hi Allard,

Thanks again for quick reply,

Yes i mean with Axon test fixtures, if we can not invoke external handler with axon test fixtures, is there any other way to unit test external event handler. As i could not write basic logic in aggregate handler as they called on every new events to rebuild the state and they are meant for state changes.

Now my external handler are getting called in tomcat, i am not sure what was the issue, i just did rewritten spring config file but nothing changed from previous :slight_smile:

If we could able to test external handler with in tomcat or standalone main class, why can not we test with unit test cases. I feel we should have provision to test external handlers similar to how we have for saga event handlers(I know sagas different).

Some of my logic i am writing in external handler, how can i unit test my external handlers.

Thanks,
UK

Hi,

both types of @EventHandler serve a completely different purpose. The external event handlers can be unit tested like any other class. Just instantiate it and invoke it.
If you want to test them in a Spring context, just wire an Event Bus and the listener and send events to the bus. Your handler will get invoked.

Cheers,

Allard

I am facing same issue and not able to resolve. The external Event Handler that I want to use to update my state database is not getting triggered. Could I please get some help on this?

Are you sure your external Event Handler is a proper spring managed bean (i.e. @Component annotated or defines as an @Bean)?

I do have @Component annotation on my Event Handler class. I am struggling with finding enough examples for axon 3.0 with spring boot. I want to use mongo as my database and kafka as distributed bus.
I am mostly struggling with Axon Configuration.

Could you please advise on how can I get started?

Hi Anshul,

Do you only have the ‘@Component’ annotation on the class, or are you also sure it’s being picked up by Spring when creating all your beans?
You need to make sure that Spring scans the package where you’ve got you event handling class set up.
If that doesn’t happen, then Axon will not know that it should wrap that class in an EventProcessor, which would led to that class not being called for event handling.

Hope this helps.

Cheers,
Steven

Hi Anshul,

with “I want to use mongo as my database”, do you mean using Mongo as your Event Store, or just as database for the query models?

Allard

Both as the event store and query model. I would try to send a sample code this weekend that I am using. Cannot send code out of my company’s network. So would try to reproduce the scenario at home.

I see, and with “kafka as distributed bus”, do you mean a Kafka based Event Bus implementation?

Yes kafka as event bus.

Then I think that’s your problem. In Axon 3, the Event Store is also your Event Bus. This means you have two event buses in your application context. It’s not unlikely that your Event Handlers subscribe to the bus (the kafka one), while your events are being published in the Event Store.

To publish events to Kafka, you should subscribe a SubscribingProcessor to the Event Store that publishes events to Kafka. If you want an EventProcessor to process events off of Kafka, you should use a Kafka based Subscribable or StreamingMessageSource.

Hope this help.
Cheers,

Allard

Thanks for your help. I was able to fix the issue using an online sample. It was an Axon Configuration Issue. I was using Embedded Event Store in my previous implementation. Changed it to default event store and cleaned up some other configs.

Hi Allard,

I am testing my command and event handlers and I have put my @EventHandlers outside the aggregate and it is invoked if I run as spring boot application,
but when I am running my test case using fixture then it is not invoked, so I found below solution given by you in this group

"If you want to test them in a Spring context, just wire an Event Bus and the listener and send events to the bus. Your handler will get invoked. "

I am not getting how to do that?, Do i need to create a EventBus Object and Autowire it into test class ? what about listener and how will i send event ?

can you please given me sample code how can i do that ?

Thanks,
Gaurav

Hi Gauruv,

The test fixtures provided by Axon are there to test Aggregates and Sagas (both with distinct implementations).
That does not mean that when you issue for example an Aggregate Test Fixture, that you should assume that an Event Handling Component will also be called.
To test the classes which contain your event handling functions, you can either write regular unit tests calling those methods directly or set up an integration test which wires up all required components to replicate a (partial) production environment.

Cheers,
Steven