0

What is the intended usage of the persist method in Akka Persistence for sequences of events? I see that there's a signature like this here:

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

but if I try to call it as in the following example

def receiveCommand= {
  case x ⇒
    val events = Seq(Event("1"), Event("2"))
    persist(events) {
      e ⇒ println(e) // here it gets printed "List(Event(1),Event(2))"
    }
}

I get printed one single event as List(Event(1),Event(2)). That is, I was expecting to handle each event separately and in the order they are given. But instead, it seems like in the following persist variant

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

the type parameter A is replaced by Seq[Event] instead of being replaced by Event and calling the sequence variant. What is the expected way to use this method?

Thanks.

ale64bit
  • 6,232
  • 3
  • 24
  • 44

1 Answers1

2

Be aware that

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

takes an

  scala.collection.immutable.Seq

as argument so you have to pass in this concrete Seq type:

 val events: scala.collection.immutable.Seq = scala.collection.immutable.Seq("event")   
 persist(events) { event =>
     log.debug("persisted " + event) 
 }
Marcus Linke
  • 179
  • 1
  • 4
  • So, when you write `Seq(1,2,3)` without specifying, it is actually a mutable sequence? – ale64bit Mar 25 '15 at 07:52
  • No. In fact it is an immutable sequence as this is the default in scala. But the problem here is that it is not typed as such. You may import scala.concurrent.immutable.Seq on top of your source file to state that all the sequences are immutable. The akka people intended to be sure that the seq must be immutable but should have choosen a different method name to prevent confusion. – Marcus Linke Mar 26 '15 at 08:51