Axon Event store distinguish old vs new events

Hi,

I’m new to the event store implementation. It seems I getting confused between events and processes.

I’m trying to build a Support request processing module using Axon framework.

As the thumb rule goes - the Aggregate replays all the past events to get to the current state so that it could apply the new events

My question is if there is a block of code inside the event handler which calls an external system(through rest or mq) apart from state modification , wont it get executed again once an new event arrives.

How can i avoid execution of code that is part of old event (which calls external system) and just execute the code that alters the state of aggregate?

Please help

Did you mean event sourcing handler or event handler? My understanding, only event sourcing handler writes the event into event store and updates aggregate state.

Can you be a little more specific about why there is a rest call on command side?

Thanks fr the reply.

I meant EventSourcingHandler , But as a matter of fact Even if I put EventHandler for the same Event it doesn’t get invoked… Not sure if both serve the same purpose

The reason I’m calling a rest api is to submit a request to an external system after an event gets invoked.
for eg.this is the flow

  1. I send a command “OnboardBillingAccount” to the Rest controller
    2.OnboardBillingAccount command generates 2 events “SubmitBillingInfo” and “SubmitAddressInfo”
  2. “SubmitBillingInfo” has a logic that makes a rest call to submit a payload and then updates the state of the Aggregate.
  3. same as for “SubmitAddressInfo” Event.

The problem I’m having is if I reload the aggregate again it re exeutes the logic for the post, I was wondering if there is a way i can just reload the state of “SubmitBillingInfo” and “SubmitAddressInfo” and not the rest api post

Hi Sandeep, Aditya,

I would suggest against calling external services from within Event Sourcing Handlers at all times.
The Event Sourcing Handler is intended to “source a given command model from the events itself has published”, and nothing else.

If you want to react to events, you will have to write a (singleton) Event Handling Component.
Axon will ensure your Event Handling Component will be backed by an Event Processor, which by default is a Tracking Event Processor (TEP for short).

Note that the TEP provides you the option to replay the events, to for example recreate Query Models.
It is suggested not to replay an Event Handling Function which introduces side effects like calling an external service.
How to deal with conditionally performing some operations when you issue a replay against a Tracking Event Processor, I suggest you to read this page of the Reference Guide.

Lastly, note that you can only have a single Event Handling Function for a given event in one class.
Thus duplicating said Event Handler in the same implementation will not make the framework call both.

Concluding, yes you can conditionally perform operations in Event Sourcing Handlers.
For this you can ask the AggregateLifecycle whether ‘it is live’, which you could have found here in the Reference Guide.
However, as said earlier, I strongly suggest against performing any kind of external service calls from within an Aggregate, ever.

Hope this clarifies things for you.

Cheers,

Steven van Beelen

Axon Framework Lead Developer

AxonIQ
Axon in Action Award 2019 - Nominate your project

This is awesome . Thanks for clarifying it Steve.