[英]Axon EventHandler does not receive event
我需要正確配置 Axon 應用程序的幫助,或者我對 axon 的理解是錯誤的。 我有兩個聚合:
我遇到的問題是“DirectoryLister”使用
AggregateLifecycle.apply(new IndexedFileEvent(...));
在“DirectoryLister”中,我可以在 EventSourcingHandler 中捕獲事件。 但是在“FileStore”內部,事件處理程序沒有反應。 我驗證了事件發布在事件總線上。 我想,我需要使用“AnnotationEventListenerAdapter”以某種方式讓 FileStore 偵聽事件總線,但是我找不到沒有 spring 的示例來說明它是如何工作的。
我正在使用沒有 Spring 的 Axon 3.4.3,並像這樣配置應用程序:
Configurer configer = DefaultConfigurer.defaultConfiguration();
configer.configureEmbeddedEventStore(c -> new InMemoryEventStorageEngine());
// configure two Aggregates
configer.configureAggregate(FileStore.class);
configer.configureAggregate(DirectoryLister.class);
// how can I register FileStore as an eventListener? Using AnnotationEventListenerAdapter?
Configuration config = configer.buildConfiguration();
// verify that event is published on the event bus
config.eventBus().subscribe(l -> l.forEach( e -> System.out.println(e.toString())));
config.start();
FileStore 類如下所示:
public class FileStore {
@AggregateIdentifier String id;
public FileStore() { }
@CommandHandler public FileStore(CreateFileStoreCommand command) {
AggregateLifecycle.apply(new FileStoreCreatedEvent(command.getId()));
}
@EventSourcingHandler public void on(FileStoreCreatedEvent event) {
id = event.getId();
}
@EventSourcingHandler public void on(IndexedFileEvent event) {
System.out.println(event.getParentPath() + "//" + event.getName() + " " + event.getSize().toString());
}
“DirectoryLister”類如下所示:
public class DirectoryLister {
@AggregateIdentifier String id;
protected DirectoryLister() { }
@CommandHandler public DirectoryLister(CreateListerCommand cmd) {
AggregateLifecycle.apply(new CreateListerEvent(cmd.getId()));
}
@CommandHandler public void handleCommand(IndexDirectoryCommand cmd) throws IOException {
FileVisitor<Path> fv = new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// this is where the event is sent!
AggregateLifecycle.apply(new IndexedFileEvent(file.getFileName().toString(),file.getParent().toString(), attrs.size()));
return FileVisitResult.CONTINUE;
}
};
Files.walkFileTree(cmd.getPath(), fv);
}
@EventSourcingHandler public void on (CreateListerEvent event) { id = event.getId(); }
// This handler is invoked.
@EventSourcingHandler public void on(IndexedFileEvent event) {
System.out.println(event.getParentPath() + "//" + event.getName() + " " + event.getSize().toString());
}
}
如果我正確地遵循,您正在嘗試處理從 Aggregate DirectoryLister
發布的事件,在 Aggregate FileStore
,對嗎?
然而,這種假設本質上是不可能的。 您所描述的聚合本質上是 CQRS 設置中的命令模型。 因此,它處理來自外部世界的命令,然后決定在該階段是否可以執行給定的命令/操作。 作為推斷是否可以處理命令的結果,會發布一個事件,通知“發生了一些事情”。
然而,命令模型並不打算直接處理事件。 它處理事件的唯一時間是“從它發布的更改中獲取自身”。 這就是為什么在聚集的事件處理程序是不是@EventHandler
軸突,而是@EventSourcingHandler
,因為它只能處理來自它自己的源事件。
參考指南還指出,處理來自給定聚合內的其他聚合的事件是不可能的(您可以在此處找到)。
因此,簡單地說,您所期望的不可能 Mathias。 您需要在中間放置一個事件處理組件,該組件對來自DirectoryLister
的給定事件做出反應,並將其轉換為您要在FileStore
上執行的命令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.