带有 axon-spring-boot-starter 的 NoHandlerForCommandException

Rod*_*ney 5 spring cqrs axon spring-boot spring-boot-starter

我正在使用 Axon + Spring Boot 创建一个简单的应用程序,只是为了确保我在实际项目中使用它之前了解 Axon 框架中的基本组件。在类 TaskAggregate 中有一个用 @CommandHandler 注释的方法,当我通过 CommandGateway 发送命令时应该调用它,但是在运行应用程序后我得到了异常:

Exception in thread "main" org.axonframework.commandhandling.NoHandlerForCommandException: No handler was subscribed to command [com.xxx.axontest.task.CreateTaskCommand]
Run Code Online (Sandbox Code Playgroud)

根据文档,@CommandHandler 注释应该足以将命令处理程序订阅到命令总线。我想我一定错过了什么。你能看看下面的代码并指出我正确的方向吗?

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xxx</groupId>
    <artifactId>axon-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <axon.version>3.0.6</axon.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
                <groupId>org.axonframework</groupId>
                <artifactId>axon-spring-boot-starter</artifactId>
                <version>${axon.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Run Code Online (Sandbox Code Playgroud)

应用程序.java

package com.xxx.axontest;

import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.eventsourcing.eventstore.EventStorageEngine;
import org.axonframework.eventsourcing.eventstore.inmemory.InMemoryEventStorageEngine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

import com.xxx.axontest.task.CreateTaskCommand;

@SpringBootApplication
public class App {

    public static void main(String[] args) {
        ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(App.class, args);
        CommandGateway commandGateway = configurableApplicationContext.getBean(CommandGateway.class);
        commandGateway.send(new CreateTaskCommand(123, "asd"));
    }

    @Bean
    public EventStorageEngine eventStorageEngine() {
        return new InMemoryEventStorageEngine();
    }

    @Bean
    public AnnotationCommandHandlerBeanPostProcessor 
 annotationCommandHandlerBeanPostProcessor() {
    return new AnnotationCommandHandlerBeanPostProcessor();
    }
}
Run Code Online (Sandbox Code Playgroud)

创建任务命令.java

package com.xxx.axontest.task;

import org.axonframework.commandhandling.TargetAggregateIdentifier;

public class CreateTaskCommand {

    @TargetAggregateIdentifier
    private int taskId;
    private String name;

    public CreateTaskCommand(int taskId, String name) {
        this.taskId = taskId;
        this.name = name;
    }

    public int getTaskId() {
        return taskId;
    }

    public String getName() {
        return name;
    }   
}
Run Code Online (Sandbox Code Playgroud)

任务创建事件.java

package com.xxx.axontest.task;

import org.axonframework.commandhandling.TargetAggregateIdentifier;

public class TaskCreatedEvent {

    @TargetAggregateIdentifier
    private int taskId;
    private String name;

    public int getTaskId() {
        return taskId;
    }

    public String getName() {
        return name;
    }

}
Run Code Online (Sandbox Code Playgroud)

任务聚合.java

package com.xxx.axontest.task;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.commandhandling.model.AggregateIdentifier;
import org.axonframework.commandhandling.model.AggregateLifecycle;
import org.axonframework.eventsourcing.EventSourcingHandler;
import org.axonframework.spring.stereotype.Aggregate;

@AggregateRoot
public class TaskAggregate {

    private Logger logger = LogManager.getLogger(TaskCreatedEvent.class);

    @AggregateIdentifier
    private int taskId;
    private String name;

    @CommandHandler
    public void handleCommand(CreateTaskCommand createTaskCommand) {
        logger.info("Command received");
        AggregateLifecycle.apply(new TaskCreatedEvent());
    }

    @EventSourcingHandler
    public void onTaskCreatedEvent(TaskCreatedEvent taskCreatedEvent) {
        logger.info("Event received");
    }

    public int getTaskId() {
        return taskId;
    }

    public void setTaskId(int taskId) {
        this.taskId = taskId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
Run Code Online (Sandbox Code Playgroud)

提前致谢。

Ste*_*ven 5

我认为您需要使用 @Aggregate 而不是@AggregateRoot. @Aggregate作为注解既是@AggregateRoot注解,也被 Spring Boot Axon Starter 模块用来表示该类的聚合工厂Repository必须被创建。此外,这意味着@CommandHandler您的聚合上的带注释的函数也将被找到并注册到CommandBus,可能会解决您捕获的异常。

否则,Allard 在 YouTube 上关于在第 3 版中启动 Axon 应用程序的网络研讨会可以为您提供一些见解。

但是,简而言之,请尝试@AggregateRoot@Aggregate:-)切换注释

但是,此外,您应该调整带@CommandHandler注释的函数,CreateTaskCommand使其成为TaskAggregate. 最后,Axon 要求您有一个无参数的构造函数供您聚合,因此还要public TaskAggregate() { }在其中添加一个构造函数。