How to handle an input event interaction?

Hello all,

In my system I’d like to handle the two interaction types: commands and events. Commands are working well but I’m struggling with events.

My idea at first was to call the apply() function but I understood based on the following link that I could only do it within a @CommandHandler: https://groups.google.com/forum/#!searchin/axonframework/Cannot$20retrieve$20current$20AggregateLifecycle$3B$20none$20is$20yet$20defined|sort:relevance/axonframework/Es2ORDr0DYI/vaODFvJPBAAJ

So my second idea was to use the Event Bus for that. The EventBus implementation I use is an EmbeddedEventStore with my own EventStorageEngine:

eventBus.publish(newGenericEventMessage<>(myEvent));

I assume this code would trigger the @EventSourcingHandler then the @EventHandler(s). But it triggered solely the @EventHandler(s).

Am I missing something here? Shouldn’t an event sent by the EventBus be caught by an @EventSourcingHandler?
If not is there any way to trigger an event without having to go first with a @CommandHandler and doing it with an apply()?

Thanks for the help!

Hello Teivah,

That’s kinda weird, since @EventSourcingHandler annotated functions are also @EventHandler annotated, since the @EventHandler lives on the @EventSourcingHandler annotation.
Additionally, you’re rephrasing the link you’ve sent incorrectly. The apply() function can only be applied from within an Aggregate (hence the fact that function lives in the AggregateLifecycle class), not necessarily a @CommandHandler annotated function only.

That being said, I think it has something to do with where the annotations are placed, so a couple of questions:

  1. Are you running a Spring Boot app?
  2. Are the @EventHandler and @EventSourcingHandler annotated functions in the same class?
  3. Since you’ve linked to this discussion, I’m guessing you don’t have any aggregates in your system, right?

Hoping we’ll get to an answer soon!

Cheers,

Steven

Hi,

apply() can be used from within your aggregate to “apply” an event, and publish it to the event bus. Applying it, means that the event is published internally to all the handlers in the aggregate instance that published the event. Note that the aggregate must be loaded (or created) using an Axon Repository instance.

If you wish to publish events from any other component, you will need to use the Event Bus instead. Simply wrap a payload using GenericEventMessage.asEventMessage(myPayload), and you’ve got yourself a message that you can publish on the Event Bus.

Hope this helps.
Cheers,

Allard