EventHandler that simply handles all events

Hi!

I was wondering whether it is possible to define an EventHandler that simply handles all possible events.

E.g. instead of defining:

@EventHandler
public void on(BankAccountCreatedEvent event) {
    repository.save(new BankAccountEntry(event.getId(), 0, event.getOverdraftLimit()));

    broadcastUpdates();
}

I would like to have:

@EventHandler
public void on(Object event) {
    ....
}

Is this possible?

Hi,
It should work according to the docs (https://docs.axonframework.org/v/3.0/part2/event-handling.html#defining-event-handlers).

That would be great, but when using “Object”, the EventHandler simply doesn’t get called at all :-/ .

I’m using Axon 3.0.6.

Hi,

it should definitely work with Object. However, note that if your class has other handlers as well, only the most specific one is actually invoked.
Alternatively, you can also define a parameter of type EventMessage<?>. That will also be invoked for all events, but doesn’t force deserialization unless you use the payload.

Cheers,

Allard

I am seeing the same thing here. Also using 3.0.6.

-Svein Arne

Seeing same thing == “working” or “handler not being invoked” ?

Same thing for me means it is not being invoked. Using the code below and then registerEventHandler through EventHandlingConfiguration.

public class EventAggregator {
public List Events = new ArrayList();

@EventHandler
public void handle(Object event) {
Events.add(event);
}
}

Are you using Spring Boot Devtools? We’ve seen issues with that before where classes were loaded using different classloaders, so that Class and Class would not be assignable to each other.

You can debug this by setting a breakpoint in the AnnotationEventListenerAdapter.handle(EventMessage) method. Probable the “canHandle” invocation returns false, because the payload of the message isn’t assignable to Object.

Allard

No and I am running this from a unit test which is initialized using this:

Configurer config = DefaultConfigurer.defaultConfiguration();
config.configureEventStore(conf -> eventStore);
EventHandlingConfiguration eventHandlerConfig = new EventHandlingConfiguration();

config.configureAggregate(TodoItem.class);
config.registerCommandHandler(c -> new TodoItemCommandHandler(new Repository©));
eventHandlerConfig.registerEventHandler(conf-> new TodoItemEventHandler());
eventHandlerConfig.registerEventHandler(conf -> new EventAggregator());

config.registerModule(eventHandlerConfig);
axon = config.buildConfiguration();
axon.start();

Sorry for wasting your time this was all my own doing as a result of “new Repository©”.

I switched to

  @EventHandler
  public void on(EventMessage<?> message) {

sendEventToClient(message, message.getIdentifier());
  }

since this fits more the needs I had. But this method wasn’t called, too.
And as Allard pointed out - this was a side effect of using Spring Boot Dev-Tools!

Too bad that the tools don’t work with axon.

Once more - thanks for your help!

Hi,

Spring Boot DevTools uses class loading tricks to do the reloading. There is configuration in place to make it work with Axon, but there is always an edge case where things don’t work as expected. We’ll try to look into it.

Cheers,

Allard