J'ai une configuration CQRS assez simple ici en utilisant Axon & Spring.Gestionnaires d'événements dans une classe distincte Axon 3.0.3
Ceci est la classe de configuration.
@AnnotationDriven
@Configuration
public class AxonConfig {
@Bean
public EventStore eventStore() {
...
}
@Bean
public CommandBus commandBus() {
return new SimpleCommandBus();
}
@Bean
public EventBus eventBus() {
return new SimpleEventBus();
}
}
Ceci est mon agrégat ...
@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!
}
}
Ceci est mon EventHandler dans un fichier séparé .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());
}
}
J'utilise le CommandGateway pour envoyer le commande de création originale. Mon CommandHandler dans l'agrégat reçoit la commande bien, mais quand j'appelle apply
dans mon agrégat, passant un nouvel événement, mon EventHandler dans la classe externe, n'est pas appelé. Seuls les EventHandlers directement à l'intérieur de la classe Aggregate sont appelés.
Si j'essaie de publier un événement directement dans EventBus, mon EventHandler externe est appelé.
Aucune idée de pourquoi mon EventHandler dans une classe Java externe n'est pas appelé lorsque j'appelle apply
dans l'agrégat?