我需要正确配置 Axon 应用程序的帮助,或者我对 axon 的理解是错误的。我有两个聚合:
- “FileStore”,应存储有关索引文件的信息
- “DirectoryLister”,为其索引的每个文件发送事件。
我遇到的问题是“DirectoryLister”使用发送事件
AggregateLifecycle.apply(new IndexedFileEvent(...));
在“DirectoryLister”内部,我能够捕获 EventSourcingHandler 中的事件。但在“FileStore”内部,事件处理程序不会使用react。我验证了该事件已发布在事件总线上。 我想,我需要使用“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
发布的事件。 ,总计FileStore
,正确吗?
然而,这个假设本质上是不可能的。 您所描述的聚合本质上是 CQRS 设置中的命令模型。 因此,它处理来自外界的命令,然后决定在该阶段是否可以执行给定的命令/操作。 作为推断命令是否可以被处理的结果,会发布一个事件来通知“发生了一些事情”。
但是,命令模型不旨在直接处理事件。
它处理事件的唯一一次是“从它已发布的更改中获取自身”。
这就是为什么聚合中的事件处理程序不是 @EventHandler
在 Axon 中,但是 @EventSourcingHandler
,因为它只能处理来自其自身来源的事件。
引用指南还指出,处理来自给定聚合内其他聚合的事件是不可能的(您可以找到 here )。
所以,简单地说,你所期待的事情是不可能的,Mathias。
您需要在其间放置一个事件处理组件,该组件对 DirectoryLister
中的给定事件使用react。并将其转换为您想要在 FileStore
上执行的命令.
关于java - Axon EventHandler 未接收事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59713176/