2017-04-20 8 views
0

여기 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가 호출되지 않는 이유는 무엇입니까?

답변

2

Axon 3에서 이벤트 저장소는 이벤트 버스에 대해 대체입니다. 그것은 기본적으로 등록 된 이벤트를 전달할뿐만 아니라이를 저장하는 특수화 된 구현입니다.

구성에 이벤트 버스와 이벤트 저장소가 모두 있습니다. 집계의 이벤트는 아마도 이벤트 저장소에 게시됩니다. 이벤트 버스에 직접 게시 할 때 핸들러에서 이벤트를 수신하므로 핸들러가 이벤트 버스에 등록됩니다.

해결 방법 : 구성에서 이벤트 버스를 제거하고 이벤트 저장소 만 사용하십시오.