여기 Axon & Spring을 사용하여 상당히 간단한 CQRS 설정이 있습니다.별도의 클래스에있는 이벤트 핸들러 Axon 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();
}
}
이
이 별도 된 .java 파일 내 이벤트 핸들러입니다@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!
}
}
내 집계 ... ...
@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가 호출됩니다.
Aggregate 내에서 apply
을 호출하면 외부 Java 클래스의 내 EventHandler가 호출되지 않는 이유는 무엇입니까?