java - 单独类中的事件处理程序 Axon 3.0.3

标签 java spring-boot axon

我使用 Axon 和 Spring 进行了相当简单的 CQRS 设置。

这是配置类。

@AnnotationDriven
@Configuration
public class AxonConfig {

    @Bean
    public EventStore eventStore() {
        ...
    }

    @Bean
    public CommandBus commandBus() {
        return new SimpleCommandBus();
    }

    @Bean
    public EventBus eventBus() {
        return new SimpleEventBus();
    }
}

这是我的汇总...

@Aggregate
public class ThingAggregate {
    @AggregateIdentifier
    private String id;

    public ThingAggregate() {
    }

    public ThingAggregate(String id) {
        this.id = id;
    }

    @CommandHandler
    public handle(CreateThingCommand cmd) {
        apply(new ThingCreatedEvent('1234', cmd.getThing()));
    }

    @EventSourcingHandler
    public void on(ThingCreatedEvent event) {
        // this is called!
    }
}

这是我的事件处理程序,位于单独的 .java 文件中...

@Component
public class ThingEventHandler {

    private ThingRepository repository;

    @Autowired
    public ThingEventHandler(ThingRepository thingRepository) {
        this.repository = conditionRepository;
    }

    @EventHandler
    public void handleThingCreatedEvent(ThingCreatedEvent event) {
        // this is only called if I publish directly to the EventBus
        // apply within the Aggregate does not call it!
        repository.save(event.getThing());
    }
}

我正在使用 CommandGateway 发送原始创建命令。聚合中的 CommandHandler 接收命令正常,但是当我在聚合中调用 apply 并传递新事件时,外部类中的 EventHandler 不会被调用。仅调用直接位于 Aggregate 类内部的 EventHandler。

如果我尝试将事件直接发布到 EventBus,则会调用我的外部 EventHandler。

知道为什么当我在聚合中调用 apply 时,外部 java 类中的 EventHandler 没有被调用吗?

最佳答案

在 Axon 3 中,事件存储是事件总线的替代品。它基本上是一个专门的实现,不仅将事件转发给订阅者,而且还存储它们。

在您的配置中,您同时拥有事件总线和事件存储。聚合的事件可能会发布到事件存储中。由于直接发布到事件总线时您会在处理程序中接收事件,因此您的处理程序会在那里订阅。

解决方案:从配置中删除事件总线并专门使用事件存储。

关于java - 单独类中的事件处理程序 Axon 3.0.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43531209/

相关文章:

java - RHEL 5/RHEL 6 上的 libfaketime 和 java

Java排序规则忽略空格

java - CompareToBuilder 的这种使用是否被认为效率低下?

java - 来自 Controller 和其他类的 Spring Boot 静态资源

spring - 在实际运行环境(例如Netflix Inc.)中使用什么技术来解决分布式事务?

java - 在中使用 Autowiring 对象

spring-boot - spring/hibernate 验证 -> 错误消息未传递给调用者?

java - 有没有办法将 2 个数据库字段映射到 JPA 中的自定义对象?

java - 构造函数 SimpleCommandBus() 不可见 -Axon

java - Axon 错误 : java. lang.IllegalArgumentException:工作单元已具有具有相同标识符的聚合