Search code examples
scalaakkaakka-persistence

Invoke persist inside another persist event handler


I have some code that invokes persist from inside another persist event handler, something like:

persist(someClassInstance){ message =>
  confirmDelivery(message.id)
  //some code
  start()
}

//Somewhere else in the code
def start(): Unit = {
  log.info("Starting")
  persist(someClassInstance){ message =>
    deliver(destination, createMessage)
    log.info("Started")
  }
}

When I run my application I see the log message "Starting" but I never see the "Started". I am wondering if this happens because I'm invoking persist inside another persist. Is this something that shouldn't be done? The documentation is not very explicit about this case.

I'm using Akka version 2.4-M1 so I suppose this could be the source of the problem, however is seems more likely to me that this is simply something that should not be done.


Solution

  • Invoking persist from another persist will block the program. The correct way is to send a message to self. And then in the code to handle that message perform the persist.