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()?
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:
Are you running a Spring Boot app?
Are the @EventHandler and @EventSourcingHandler annotated functions in the same class?
Since you’ve linked to this discussion, I’m guessing you don’t have any aggregates in your system, right?
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.