无法序列化从通用特征派生的通用案例类

时间:2014-07-31 19:50:45

标签: java scala generics serialization akka-persistence

我使用以下代码:

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来持久化这些事件并重建演员的状态。但是,在调用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,但这并没有太大帮助。

无论如何得到了解决方案吗?也许其他序列化器?或者另一种制作一组通用事件/命令/方法的方法?

1 个答案:

答案 0 :(得分:2)

找到它。

要调试此类问题,请将此添加到VM选项中;

-Dsun.io.serialization.extendedDebugInfo=true

原来sealed trait ScanList[E]需要扩展Serializable

P.S。如果您正在进行事件采购并且您的域是有机的,那么将Events / Command放在scala对象下并不是一个好主意:)您将无法再反序列化您的父对象,使您的持久数据无用。