I have this scenario when I want to create an aggregate based on an event. So from the eventHandler, I send the command to create the aggregate and it fails with “Invalid sequence number”. But if I send the same command from a normal method (not axon handlers), it works fine. So what is the best practice so create aggregate based on an event?
you did not mention what kind of event handler you used but it seems you used the event sourcing handler on the aggregate to send a command to the same aggregate? You should never send a command from a aggregate event handler. If you do not check if the aggregate isLife you would send it over and over and over again everytime the aggregate gets loaded. To the same aggregate you could “send” a command as last action in the command handler after the apply() by simply calling the @CommandHandler annotated method in the same aggregate with the command as parameter.
Hi Marc. You got the scenario wrong. Sorry if the question was not clear enough. But I meant any method annotated with @EventHandler which listens to some events (those events are coming from another domain another spring boot application). So I wanna listen to those events in my spring boot application and from within the event listener method, send the command to create a whole new aggregate and it always fails. I hope now it is more clear.
You should never create an aggregate from an event handler as event handlers are replayed and you would create a new aggregate every time the handler was replayed. Instead you should create a saga to handle the event and send the new command from there.
Hi Tyler, Thanks for the input. I do not see how a SagaEventHandler could be different from a normal EventHandler in this case. If you send a command to create an aggregate from a SagaEventHandler it will fail again. May you elaborate more please? Your point about replaying event and recreation of aggregate over and over again is valid , but you can simply swallow the exception in a try catch. That’s why I asked what is best practice in this kind of scenarios. Also technically I really like to understand why an aggregate creation command which is send from EventHandler, fails but the same command from a normal java method works
I tried to reproduce your scenario and didn’t face any issue. That leads me to think that maybe you are trying to create an Aggregate with the same id that the one that just fires the event. Could you please post here the sequence of commands/events that you have in your case ?
Hi Jérôme, thank you very much for the input. Yes you are right. Both of these two aggregates which are in different domains and different spring boot apps are using the same aggregate Id
which is userId of logged-in user. So what’s wrong with this? Right now, I have both aggregates working if command to create aggregate 2 is sent from the place that command to create
aggregate 1 is sent. But if I want to send aggregate 2 creation command from EventHandler for Aggregate1createdEvent, then it fails. I need userId as my aggregateId because it helps to
detect situations when a command is sent multiple times by a user and then just first one succeed and all others fails by axon which is what I want. So do you have any suggestion?
the aggregate identifier is used to identify the stream of events that belongs to the aggregate. If you have to Aggregates with the same ID, they will attempt to write to the same stream. It is generally recommended not to reuse identifiers between aggregates. In your case, you probably want the identifier to be deterministic, so that you can detect multiple instances. One way to achieve that is by pre/suffixing the ID with something, such as the name of the aggregate.
Hope this helps.