我正在使用以下代码:
trait ScanList[E] {
sealed trait Event
case class Added(item: E) extends Event
case class Requeued(item: E) extends Event
case class Processed(item: E, at: DateTime) extends Event
case class Dequeued(item: E) extends Event
// some unimplemented methods here..
}
object CharacterScanList extends ScanList[CharacterMetadata] {
// some implemented methods here..
}
使用 Akka Persistence 持久化这些事件并重构 actor 的状态。但是,在致电persist(ev)
时,我收到以下异常:
java.io.NotSerializableException: nl.mdj.wowcrafters.informant.CharacterScanList$
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347)
at akka.serialization.JavaSerializer$$anonfun$toBinary$1.apply$mcV$sp(Serializer.scala:129)
at akka.serialization.JavaSerializer$$anonfun$toBinary$1.apply(Serializer.scala:129)
at akka.serialization.JavaSerializer$$anonfun$toBinary$1.apply(Serializer.scala:129)
at scala.util.DynamicVariable.withValue(DynamicVariable.scala:58)
at akka.serialization.JavaSerializer.toBinary(Serializer.scala:129)
at akka.persistence.serialization.MessageSerializer.akka$persistence$serialization$MessageSerializer$$payloadBuilder$1(MessageSerializer.scala:151)
at akka.persistence.serialization.MessageSerializer.akka$persistence$serialization$MessageSerializer$$persistentPayloadBuilder(MessageSerializer.scala:159)
at akka.persistence.serialization.MessageSerializer.akka$persistence$serialization$MessageSerializer$$persistentMessageBuilder(MessageSerializer.scala:136)
at akka.persistence.serialization.MessageSerializer.toBinary(MessageSerializer.scala:53)
at akka.serialization.Serialization$$anonfun$serialize$1.apply(Serialization.scala:90)
at akka.serialization.Serialization$$anonfun$serialize$1.apply(Serialization.scala:90)
at scala.util.Try$.apply(Try.scala:191)
at akka.serialization.Serialization.serialize(Serialization.scala:90)
at akka.persistence.journal.leveldb.LeveldbStore$class.persistentToBytes(LeveldbStore.scala:96)
at akka.persistence.journal.leveldb.LeveldbJournal.persistentToBytes(LeveldbJournal.scala:20)
at akka.persistence.journal.leveldb.LeveldbStore$class.akka$persistence$journal$leveldb$LeveldbStore$$addToMessageBatch(LeveldbStore.scala:102)
at akka.persistence.journal.leveldb.LeveldbStore$$anonfun$writeMessages$1$$anonfun$apply$1.apply(LeveldbStore.scala:44)
at akka.persistence.journal.leveldb.LeveldbStore$$anonfun$writeMessages$1$$anonfun$apply$1.apply(LeveldbStore.scala:44)
at scala.collection.Iterator$class.foreach(Iterator.scala:743)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1174)
at scala.collection.IterableLike$class.foreach(IterableLike.scala:72)
at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
at akka.persistence.journal.leveldb.LeveldbStore$$anonfun$writeMessages$1.apply(LeveldbStore.scala:44)
at akka.persistence.journal.leveldb.LeveldbStore$$anonfun$writeMessages$1.apply(LeveldbStore.scala:44)
at akka.persistence.journal.leveldb.LeveldbStore$class.withBatch(LeveldbStore.scala:88)
at akka.persistence.journal.leveldb.LeveldbJournal.withBatch(LeveldbJournal.scala:20)
at akka.persistence.journal.leveldb.LeveldbStore$class.writeMessages(LeveldbStore.scala:44)
at akka.persistence.journal.leveldb.LeveldbJournal.writeMessages(LeveldbJournal.scala:20)
at akka.persistence.journal.SyncWriteJournal$$anonfun$receive$1$$anonfun$1.apply$mcV$sp(SyncWriteJournal.scala:27)
at akka.persistence.journal.SyncWriteJournal$$anonfun$receive$1$$anonfun$1.apply(SyncWriteJournal.scala:27)
at akka.persistence.journal.SyncWriteJournal$$anonfun$receive$1$$anonfun$1.apply(SyncWriteJournal.scala:27)
at scala.util.Try$.apply(Try.scala:191)
at akka.persistence.journal.SyncWriteJournal$$anonfun$receive$1.applyOrElse(SyncWriteJournal.scala:27)
at akka.actor.Actor$class.aroundReceive(Actor.scala:465)
at akka.persistence.journal.leveldb.LeveldbJournal.aroundReceive(LeveldbJournal.scala:20)
at akka.actor.ActorCell.receiveMessage(ActorCell.scala:516)
at akka.actor.ActorCell.invoke(ActorCell.scala:487)
at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:238)
at akka.dispatch.Mailbox.run(Mailbox.scala:220)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
似乎 Java 序列化程序无法处理派生的通用案例类?我已经尝试过sealed trait Event extends Serializable
,但这并没有太大帮助。
无论如何有解决方案吗?也许其他序列化程序?还是另一种制作一组通用事件/命令/方法的方法?