Same Command to different AR Types

Hey,

To handle a recovery situation with our system, our ErrorHandler can fire a Command onto the CommandBus asking an AR to temporarily suspend itself. In turn, it fires an event to acknowledge this, and an event handler in the AR sets “suspended = true”.

I would ideally like to use the same Command for different AR types (so they can share the “recovery / suspend” code), but it seems like only one CommandHandler per Command can be registered.

I can’t just use an interceptor as the state needs to maintained across restarts (in the EventStore).

Any thoughts?

JAmes

I share with you my first two thoughts : )

Since an aggregate in the axon event store (at least it’s JPA implementation) is identified by it’s type and aggId, if you would have several command handlers to handle the same command you would effectively have to try to load the given aggId one time for each aggregate type. This you can do yourself, by having a GenericCommandHandler which handles generic commands, for example SuspendAggregateCommand. This command handler would then have to know which are the possible aggregates that can be suspended and try to load them, one after another from their respective repositories (given an aggId I assume it can not both be for example a Customer and an Order).

Another way would be to have an abstract base class for SuspendAggregateCommand which contains the aggId, and only add information about the aggregate type (given by the command type) in the concrete commands.

Sebastian

Hi James,

here’s a thrid thought, but it only applies to Axon 2. In Axon 2, the payload of a command (the obect you’re sending) is separated from the command name. So it’s possible to send the same payload under different names.

On you command handler, you would need to specify the name explicitly:
@CommandHandler(commandName = “SuspendAggregateTypeA”)
public void myHandler(…) {}

When sending a command, you need to explicitly create a CommandMessage:

command = new GenericCommandMessage(“SuspendAggregateTypeA”, actualCommand, metaData);

Dispatching that on the command bus should provide the feature you require.
Note that the default command name used by Axon is the fully qualified class name of the payload.
Also note that you should not use the same command name more than once, as one handler will overrule the other.

Cheers,

Allard

Ah cool! I think I’ve worked this out now… Thanks! :slight_smile:

JAmes