0
votes

I have defined a Microservice:

AccountManagementService

  • Handles CreateAccountCommand
  • CreateAccountCommand shoots AccountCreatedEvent
  • The new Account is then stored in mysql db
  • Axon Server is used to store the event

Now I want another microservice B (that is connected to the same Axon Server) to handle the AccountCreatedEvent too.

enter image description here

Can I simply put

@EventHandler
protected void on(AccountCreatedEvent event){
}

in the second microservice B (for example in an Aggregate)? I tried that under the assumption that the event is stored in Axon Server and my second microservice automatically listens to that published event. It didn't work unfortunately.

How can microservice B subscribe to events (published by AccountManagementService) in the Axon Server?

1

1 Answers

1
votes

The key issue you are expiriencing stems from one of your final sentences:

Can I simply put [insert-code-snippet] in the second microservice B (for example in an Aggregate)?

Well, yes you can simply add an @EventHandler annotated function in a regular component, but not in an Aggregate. The Aggregate functionality provided by Axon Framework is aimed towards being a Command Model. As would be used when employing CQRS really.

This makes the Aggregate a component which can solely handle command messages.

The only exception to this, is when you are adding Event Sourcing in the mix too. When Event Sourcing, you will source a given Command Model based on the events it has published. Thus introducing an @EventSourcingHandler annotated function inside your aggregate will only ever handle the events which originated from it.

To circle back to your original question, yes you can simply put the:

@EventHandler
protected void on(AccountCreatedEvent event){
    // My event handling operation
}

in (micro)service B. But, that method cannot reside in an Aggregate. This is by design, to follow the CQRS and Event Sourcing paradigm as intended.

Any other component will do just fine though. If you are combining Axon with Spring, simply having the components with your event handling functions is sufficient. If you are not using Spring, you will have to register your components with event handlers to the Configurer or EventProcessingConfigurer.

Hoping this clarifies things for you.