Same command handled by either a constructor or a method

Hello,

I’m wondering whether it is possible to have the same command handled in a constructor and a method?

As an example, let’s imagine an aggregate made of:

`
@CommandHandler
public TestAggregate(CustomerCommand command) {
apply(new CustomerCreatedEvent(command.getId(), command.getName()));
}

@CommandHandler
public void on(CustomerCommand command) {
apply(new CustomerUpdatedEvent(command.getId(), command.getName()));
}
`

What I expect is:

  • If the aggregate instance does not exist, the command is caught by the constructor which create an aggregate instance
  • Otherwise, the command is caught by the on() method to enrich the existing aggregate instance

Is that something possible in Axon? It seems the command is always caught by the constructor even in such cases:

fixture. given(new CustomerCreatedEvent("001", "a")). when(new CustomerCommand("001", "b")). expectEvents(new CustomerUpdatedEvent("001", "b"));

Thanks

Hi Teivah,

this is not possible, currently. If you want “update or create” semantics, you would have to either send 2 commands (when the first fails) or create an external command handler (in a separate bean), which attempts to load an aggregate and -if not found- creates a new one.

Cheers,

Allard

Hi Allard,

Fair enough, I’ll go with the second option then because the first is not applicable in my context. Yet this would have been nice option I guess.

Thank you