我使用spring boot和axon为例,我实现了快照功能,使用下面的代码可以很好地工作,经过3次事件后,我在数据库中找到了表snapshot_event_entry中的数据
@Configuration
@AutoConfigureAfter(value = { AxonAutoConfiguration.class })
public class AxonConfig {
@Bean
public SnapshotTriggerDefinition catalogSnapshotTrigger(Snapshotter snapshotter) {
return new EventCountSnapshotTriggerDefinition(snapshotter, 3);
}
}
@Aggregate(snapshotTriggerDefinition = "catalogSnapshotTrigger")
public class CatalogAggregate { }我的问题是,有没有一种方法可以按需进行快照?这意味着我希望实现一个api来进行快照,而不是在发生3个事件后自动执行
发布于 2020-09-10 15:26:03
没有任何已经到位的东西。实现你需要的一种方法是创建一个专用的命令,例如PerformShapshotCmd,它将携带aggregateId信息,并将一个@CommandHandler放入你的聚合中。然后,您可以让Spring自动连接Snapshotter实例bean,并调用scheduleSnapshot(Class<?> aggregateType, String aggregateIdentifier)方法。
下面是一些可以指导你的代码片段。
data class PerformShapshotCmd(@TargetAggregateIdentifier val id: String) @CommandHandler
public void handle(PerformShapshotCmd cmd, Snapshotter snapshotter) {
logger.debug("handling {}", cmd);
snapshotter.scheduleSnapshot(this.getClass(), cmd.getId());
}您还应该在配置中定义一个Snapshotter类型的Bean
@Bean
public SpringAggregateSnapshotterFactoryBean snapshotter() {
SpringAggregateSnapshotterFactoryBean springAggregateSnapshotterFactoryBean = new SpringAggregateSnapshotterFactoryBean();
//Setting async executors
springAggregateSnapshotterFactoryBean.setExecutor(Executors.newSingleThreadExecutor());
return springAggregateSnapshotterFactoryBean;
}请注意,您的commandHandler的第一个参数需要是命令,否则框架将在启动时报告异常。
https://stackoverflow.com/questions/63811309
复制相似问题