Search code examples
scalaakkaakka-persistence

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

For above method, is param passed to handler guaranted to be exactly the same(with same identifyHashCode) instance as the persisted event ?

According to my few tests, they are actually same, but I don't know akka will always ensure this even in the future release


Solution

  • From the source code, we can see they are exactly the same.

    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()))
    }
    

    You can see the same event be passed to AtomicWrite for persist & also passed to StashingHandlerInvocation for handle. I didn't see any reason for akka team to change this later. But no one can promise for future release even the maintainer I think, maybe you need to wait the man from lightbend. FYI.