akka persist callback param 是否与持久事件相同

Is akka persist callback param just the same intance as persisted event

def persist[A](event: A)(handler: (A) ⇒ Unit): Unit

def persistAll[A](events: Seq[A])(handler: (A) ⇒ Unit): Unit

对于上述方法,传递给 handler 的参数是否保证与持久化的 event 完全相同(具有相同的 identifyHashCode)实例?

根据我的几次测试,它们实际上是相同的,但我不知道即使在未来的版本中,akka 也会始终确保这一点

从源码来看,完全一样

def persist[A](event: A)(handler: A => Unit): Unit = {
  if (recoveryRunning) throw new IllegalStateException("Cannot persist during replay. Events can be persisted when receiving RecoveryCompleted or later.")
  pendingStashingPersistInvocations += 1
  pendingInvocations addLast StashingHandlerInvocation(event, handler.asInstanceOf[Any => Unit])
  eventBatch ::= AtomicWrite(PersistentRepr(event, persistenceId = persistenceId,
    sequenceNr = nextSequenceNr(), writerUuid = writerUuid, sender = sender()))
}

您可以看到相同的 event 被传递给 AtomicWrite 用于持久化,也被传递给 StashingHandlerInvocation 用于句柄。我没有看到 akka 团队稍后更改此设置的任何理由。但是没有人可以保证未来的发布,即使是我认为的维护者,也许你需要等待来自 lightbend 的人。仅供参考。