No handler registered for the command

Hello there :wave:

I am trying to receive via Spring Boot Web RestAPI commands, which dispatched to an CommandGateway and handled via an CommandHandler (Aggregate).

My Problem is, that if the command is dispatched, I get an Exception that no Handler for that Command is registered.

@Configuration
class CommandBusBean {
    @Bean
    fun commandBus() : CommandBus = SimpleCommandBus.builder().build()
}
@Configuration
class CommandGatewayBean {
    @Bean
    fun commandGateway(@Autowired commandBus: CommandBus): CommandGateway = DefaultCommandGateway.builder()
        .commandBus(commandBus)
        .build();
}
@Aggregate
class AddWasteTypeCommandHandler() {

    @AggregateIdentifier
    lateinit var wasteTypeId: String

    @CommandHandler
    @CreationPolicy(AggregateCreationPolicy.CREATE_IF_MISSING)
    fun handle(addWasteTypeCommand: AddWasteTypeCommand) {
        val event = AddWasteTypeEvent(
            addWasteTypeCommand.wasteTypeId,
            addWasteTypeCommand.name,
            addWasteTypeCommand.description
        )
        AggregateLifecycle.apply(event)
    }
}
data class AddWasteTypeCommand(
    @TargetAggregateIdentifier
    val wasteTypeId: String,
    val name: String,
    val description: String
)

Hi Christopher,
Everything seems to be alright when I check the classes. Is the AddWasteTypeCommandHandler class in the same package (or a sub-package) as the package with the main application class?

Marc

No, they are in dfferent packages.

That is probably the reason why the command handler is not found. The discovery mechanism to find Spring beans (aggregate annotated classes are spring beans) starts from the package where the Spring application is defined. So, suppose your main class is in package a.b.c. In that case, only beans in the same package or any subpackage are found, not those defined in package a.b.d. If you want to include another package, you can either add a basePackages attribute to the SpringBootApplication annotation or add a ComponentScan annotation.

1 Like

So if I throw an command in my Controller, only command handlers in the same package or sub-package are noticed?

I was referring to packages below the main application class, in your case this looks like the SpringKotlinRestfulApiSampleApplication class. Can you share the project so I can check?

Of course :slight_smile: I have exported the project as zip file. You can access it, via these file sharing link: Data package from February 21st. - FileTransfer.io

Thank you a lot for your help!

I found several issues with the project. First it uses the axon-spring module, but the auto registration of Axon annotated aggregates and methods requires the axon-spring-boot-starter module.Using this module, the application will try to use Axon Server as its event store and to distribute commands and queries. If you want to run standalone, you can set the following property in application.properties:

axon.axonserver.enabled=false

Also, the h2 version that you included is too old. If you change it to version 1.4.197 it works better.

Finally, the command handler either needs to be on the constructor or have the annotation @CreationPolicy(AggregateCreationPolicy.CREATE_IF_MISSING) to create an instance of the aggregate. Also, to be able to restore the state of the aggregate, you must set all properties in the aggregate in event sourcing handlers, so instead of setting this.wasteTypeId in the command handler, set it in a method like this:

@EventSourcingHandler
    fun on(event: AddWasteTypeEvent) {
        this.wasteTypeId = event.wasteTypeId
    }