2017-04-20 66 views
0

我已经有一个相当简单的CQRS设置使用轴突&春天。事件处理程序在一个单独的类轴突3.0.3

这是配置类。

@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送原始创作命令。我在Aggregate中的CommandHandler收到的命令很好,但是当我在Aggregate中调用apply时,传递一个新的Event,我的EventHandler在外部类中不会被调用。只有直接在Aggregate类中的EventHandlers被调用。

如果我尝试直接向EventBus发布Event,则会调用我的外部EventHandler。

任何想法为什么我在外部java类中的EventHandler没有被调用,当我在Aggregate中调用apply

回答

2

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

在您的配置中,您有一个事件总线和一个事件存储。 Aggregate的事件可能发布到Event Store。由于您在直接发布到事件总线时在处理程序中收到事件,因此您的处理程序将在此处订阅。

解决方案:从您的配置中删除Event Bus并专门使用Event Store。

相关问题