基于轴突状态的聚合是否有指定@CreatedDate 和@LastModifiedDate 的方法?

Do axon state-based aggregates have a way of specifying @CreatedDate and @LastModifiedDate?

创建 Axon JPA 基于状态的聚合时,有没有办法将某些字段标记为 @CreatedDate 和 @LastModifiedDate(使用 spring data jpa 可能)?

换句话说,Axon 是否具有这样的功能:如果聚合体的任何状态发生变化,Axon 会自动更新 @LastModifiedDate,而我们不必在每个 @CommandHandler 中重复它?

尝试在聚合中使用 @CommandHandlerInterceptor 拦截所有命令并设置 lastModifiedDate 字段。

@CommandHandlerInterceptor
public Object intercept(Object myCommand, InterceptorChain interceptorChain) throws Exception {

   this.lastModifiedDate = Instant.now();

   return interceptorChain.proceed();

}

我认为正确的解决方案是实施 Axon 的 HandlerEnhancerDefinition 接口来更新这些字段。通过这种方式,您可以从事件存储中持久保存的事件中获取相同的时间戳 (Instant),并在 state-stored 聚合上使用它来使它们匹配。

我写了一篇博客 post,其中包含一个工作示例,详细解释了如何执行此操作:https://michael.jeszenka.com/automatically-updating-timestamp-fields-for-axon-state-stored-aggregates/

本质上,您需要实施 wrapHandler() 方法来指定您希望使用增强器包装哪些类型的事件处理程序。然后你需要定义一个包装器 class 来执行你想要的行为,在我们的例子中它会自动处理 state-stored 聚合的时间戳。这个包装器 class 将需要实现 Object handle(Message<?> message, T target) 方法,这将使我们能够从元数据中获取事件时间戳并使用它来设置 state-stored 聚合字段。