【问题标题】:NoHandlerForCommandException with axon-spring-boot-starterNoHandlerForCommandException 与 axon-spring-boot-starter
【发布时间】:2018-03-29 19:28:41
【问题描述】:

我正在使用 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]

根据文档,@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>

App.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();
    }
}

CreateTaskCommand.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;
    }   
}

TaskCreatedEvent.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;
    }

}

TaskAggregate.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;
    }

}

提前致谢。

【问题讨论】:

  • 一件重要的事情:事件和命令应该是不可变的(没有设置器)
  • 来自文档:“CommandHandlers 需要订阅 CommandBus 才能接收命令”
  • @ConstantinGalbenu,我根据您的建议删除了 setter。

标签: spring spring-boot cqrs axon spring-boot-starter


【解决方案1】:

基于 cmets

我想分享修改后的实际代码。

@Aggregate
public class TaskAggregate {

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

    @AggregateIdentifier
    private int taskId;

    private String name;

    TaskAggregate(){ // default constructor needed for axon
    }

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

    @EventSourcingHandler
    public void onTaskCreatedEvent(TaskCreatedEvent taskCreatedEvent) {
        logger.info("Event received");
        this.name = taskCreatedEvent; // use this to set the aggregate meber than get and setter.
    }

}

@CommandHandlerCreateTaskCommand 注释函数作为 TaskAggregate 的构造函数。最后,Axon 要求您有一个 no-arg 构造函数 用于聚合,因此还要在其中添加一个 public TaskAggregate() { } 构造函数。

【讨论】:

    【解决方案2】:

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

    否则,Allard 在 YouTube 上的 webinars 用于在版本 3 中启动 Axon 应用程序可以为您提供一些见解。

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

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

    【讨论】:

    • 好收获。我试过了,现在我得到 Command 'com.xxx.axontest.task.CreateTaskCommand' 导致 org.axonframework.commandhandling.model.AggregateNotFoundException(在事件存储中找不到聚合)
    • 调整了注释以指出您可能应该让CreateTaskCommand 由构造函数而不是常规函数处理。此外,还有一个无参数构造函数,因为 Axon 需要这样做。
    • 我在运行测试时遇到了同样的问题。有一个聚合工作正常,但没有发现另一个聚合......我使用了相同的注释@Aggragate、空构造函数和 commandHandler 构造函数。它在应用程序运行时注册,但在运行测试时不注册。任何想法?谢谢
    • 您指的是@Pietro 什么样的测试?对于聚合测试,Axon 提供了我建议您使用的专用测试夹具。你可以在这里找到更多关于它们的信息:docs.axoniq.io/reference-guide/axon-framework/testing/…
    • 如果是使用 Spring Boot 的集成测试,则必须确保连接所需的 Axon 组件,以及包含命令处理功能的组件。
    【解决方案3】:

    基于以上代码,几点说明:

    • 您不需要提供 AnnotationCommandHandlerBeanPostProcessor。事实上,指定一个可能会干扰 Axon/Spring Boot 自动配置的正常运行
    • 创建新聚合实例的命令应放在构造函数中。目前还没有调用该方法的实例。请注意,您将(也)必须指定一个无参数构造函数。
    • taskId 应该由@EventSourcingHandler 设置。 Getter 和 Setter 不属于(事件来源的)聚合
    • 在事件中,您不需要指定@TargetAggregateIdentifier。它们只是命令的手段。

    鉴于您提供的代码,我无法解释此异常,但显式定义的 AnnotationCommandHandlerBeanPostProcessor 可能会妨碍您。

    [已编辑] Steven 正确地注意到了 @AggregateRoot 注释。它应该是@Aggregate。上面的cmets仍然有效,但与异常没有直接关系[/Edited]

    【讨论】:

    • 有点困惑,因为documentation 说:Axon 将自动向命令总线注册所有带有 CommandHandler 注释的方法,如果没有,则设置存储库。 没有提到构造函数。但是,正如您所建议的,在构造函数中使用 @CommandHandler 注释而不是方法,可以解决问题。非常感谢。
    【解决方案4】:

    您还需要将处理程序注册到命令总线。我发现 this tutorial 应该可以帮助你。从那里快速强调:

    @Configuration 
    public class AppConfiguration { 
    
        @Bean  
        public SimpleCommandBus commandBus() { 
            SimpleCommandBus simpleCommandBus = new SimpleCommandBus(); 
            // This manually subscribes the command handler: 
            // DebitAccountHandler to the commandbus.  
            simpleCommandBus.subscribe(DebitAccount.class.getName(), new DebitAccountHandler()); 
            return simpleCommandBus;  
        }
    }
    

    附注一件重要的事情:事件和命令应该是不可变的(没有设置器)

    【讨论】:

    • 你提到的教程使用的是axon 2.4.1。我正在使用 3.0.6。在axon's documentation,聚合配置部分,他们说:Axon 将自动向命令总线注册所有 @CommandHandler 注释方法,如果不存在,则设置存储库。
    • 在您提到的教程中,他们还说:如果将 Axon 与 Spring 一起使用,请使用 AnnotationCommandHandlerBeanPostProcessor,它允许我们将带有 @CommandHandler 注释的方法的 Spring beans 转换为命令处理程序。
    • 在我的项目中,命令处理程序位于聚合中。在 axon 3.0.6 中没有我可以继承的 CommandHandler 接口。
    猜你喜欢
    • 2016-01-29
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 2015-07-07
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    相关资源
    最近更新 更多