【发布时间】:2020-01-13 08:50:29
【问题描述】:
我需要正确配置 Axon 应用程序的帮助,或者我对 axon 的理解是错误的。我有两个聚合:
- “FileStore”,存储索引文件的信息
- “DirectoryLister”,它为它索引的每个文件发送事件。
我遇到的问题是“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());
}
}
【问题讨论】: