Aggregate Constructor as Command Handler using Distributed Command bus

I am using Axon v3.0.7 and have below aggregate and command.



class AggregateRoot {

private String id;

public AggregateRoot(CreateAggregateCmd cmd) throws Exception {

String uid= UUID.randomUUID().toString();

AggregateCreatedEvent event = new AggregateCreatedEvent(appId);


class AggregateCreatedEvent {
//No targetidentifier field, as we construcing id in AggregateRoot constructor
String description;


I am using Aggregate constructor as Command Handler.

The same code works well using SimpleCommandBus ( with Spring Boot).

However, When upgraded to DistribuedCommandBus its throwing below error.

org.axonframework.commandhandling.distributed.CommandDispatchException: The command [CreateAggregateCmd] does not contain a routing key.
at org.axonframework.commandhandling.distributed.AbstractRoutingStrategy.getRoutingKey(
at org.axonframework.springcloud.commandhandling.SpringCloudCommandRouter.findDestination(
at org.axonframework.commandhandling.distributed.DistributedCommandBus.dispatch(
at org.axonframework.commandhandling.gateway.AbstractCommandGateway.send(
at org.axonframework.commandhandling.gateway.DefaultCommandGateway.send(
at org.axonframework.commandhandling.gateway.DefaultCommandGateway.send(

We can update command to have TargetAggregateIdentifier and fix this issue.

Will there any other workaround to make same code work for both SimpleCommandBus & DistributedCommandBus ?




It is our recommended practice to generate the aggregate ID on the client and add it to the command. If it is possible to upgrade your commands, that would be the best way forward.

Alternatively, it is possible to configure the SpringCloudCommandRouter to generate a random key for commands that do not have a routing key. The SpringCloudCommandRouter constructors have a RoutingStrategy parameter. See SpringCloudAutoConfiguration#springCloudCommandRouter to see how the SpringCloudCommandRouter is constructed by default. The AnnotationRoutingStrategy is used which has a constructor that allows you to set the UnresolvedRoutingKeyPolicy.

Hope this helps, Oscar