Aggregate Constructor as Command Handler using Distributed Command bus

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

`

`

@Aggregate
class AggregateRoot {

@AggregateIdentifier
private String id;

@CommandHandler
public AggregateRoot(CreateAggregateCmd cmd) throws Exception {

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

AggregateCreatedEvent event = new AggregateCreatedEvent(appId);
event.setAgentId(cmd.getAgentId());
apply(event);
}

}

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(AbstractRoutingStrategy.java:57)
at org.axonframework.springcloud.commandhandling.SpringCloudCommandRouter.findDestination(SpringCloudCommandRouter.java:91)
at org.axonframework.commandhandling.distributed.DistributedCommandBus.dispatch(DistributedCommandBus.java:124)
at org.axonframework.commandhandling.gateway.AbstractCommandGateway.send(AbstractCommandGateway.java:79)
at org.axonframework.commandhandling.gateway.DefaultCommandGateway.send(DefaultCommandGateway.java:95)
at org.axonframework.commandhandling.gateway.DefaultCommandGateway.send(DefaultCommandGateway.java:143)

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 ?

Thanks

`

Hi,

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