我使用 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/