3

我正在尝试使用 akkas 持久性模块(2.3.0),不幸的是,当我发送包含枚举的持久性消息时,我得到 java.io.NotSerializableException. 这是我的朴实的例子:

object TestEnum extends Enumeration with Serializable {
  type TestEnum = Value
  val test = Value("test")
}

class TestProcessor extends Processor with Logging {
  override def receive: Actor.Receive = {
    case PersistenceFailure(payload, sequenceNr, cause) =>
      log.error(s"error when reciving persistent message[$payload]", cause)
    case a =>
      log.error(s"test proc recived message [$a]")
  }
}
val a = sys.actorOf(Props[TestProcessor])
a ! Persistent(TestEnum.test)

这以

 error when reciving persistent message[test]java.io.NotSerializableException:         scala.slick.driver.JdbcTypesComponent$MappedJdbcType$$anon$1
    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.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:57)
    at akka.serialization.JavaSerializer.toBinary(Serializer.scala:129)
    at akka.persistence.serialization.MessageSerializer.persistentPayloadBuilder(MessageSerializer.scala:111)
    at akka.persistence.serialization.MessageSerializer.akka$persistence$serialization$MessageSerializer$$persistentMessageBuilder(MessageSerializer.scala:97)
    at akka.persistence.serialization.MessageSerializer.toBinary(MessageSerializer.scala:45)
    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:161)
    at akka.serialization.Serialization.serialize(Serialization.scala:90)
    at akka.persistence.journal.leveldb.LeveldbStore$class.persistentToBytes(LeveldbStore.scala:98)
    at akka.persistence.journal.leveldb.LeveldbJournal.persistentToBytes(LeveldbJournal.scala:19)
    at akka.persistence.journal.leveldb.LeveldbStore$class.akka$persistence$journal$leveldb$LeveldbStore$$addToMessageBatch(LeveldbStore.scala:104)
    at akka.persistence.journal.leveldb.LeveldbStore$$anonfun$writeMessages$1$$anonfun$apply$1.apply(LeveldbStore.scala:48)
    at akka.persistence.journal.leveldb.LeveldbStore$$anonfun$writeMessages$1$$anonfun$apply$1.apply(LeveldbStore.scala:48)
    at scala.collection.Iterator$class.foreach(Iterator.scala:727)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1157)
    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:48)
    at akka.persistence.journal.leveldb.LeveldbStore$$anonfun$writeMessages$1.apply(LeveldbStore.scala:48)
    at akka.persistence.journal.leveldb.LeveldbStore$class.withBatch(LeveldbStore.scala:90)
    at akka.persistence.journal.leveldb.LeveldbJournal.withBatch(LeveldbJournal.scala:19)
    at akka.persistence.journal.leveldb.LeveldbStore$class.writeMessages(LeveldbStore.scala:48)
    at akka.persistence.journal.leveldb.LeveldbJournal.writeMessages(LeveldbJournal.scala:19)
    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:161)
    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:19)
    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:744)

任何人都知道这里发生了什么以及如何解决它?

4

1 回答 1

2

我想的更复杂。根据 Patrik 的建议,我已将所有参与对象定义为不同文件中的顶级类/对象。并且测试示例开始工作。最后我发现了为什么会发生错误:

我正在使用 slick 与我的 DB 进行通信,因此我声明了非常有用的 Enumeration 基类和默认映射器,它允许 slick 在不编写转换器的情况下使用 Enumeration

abstract class DBEnum extends Enumeration {
  implicit val enumMapper = MappedJdbcType.base[Value, String](_.toString, this.withName(_))
}

然后我从那个类派生并创建了

object AccountType extends DBEnum{
  type AccountType = Value
  val ADMINISTRATOR = Value("administrator")
  val REGULAR = Value("regular")
  val ADMINISTRATOR_SPONSORED = Value("administrator_sponsored")
}

我的实际测试示例是

a ! Persistent(TestEnum.test)
a ! Persistent(AccountType.ADMINISTRATOR)

并且似乎 TestEnum 开始有 enumMapper (可能是因为一些奇怪的隐式转换 - 也许有人可以解释它是如何可能的)不能被序列化(它没有实现可序列化)。

改变

implicit val enumMapper = MappedJdbcType.base[Value, String](_.toString, this.withName(_))

implicit def enumMapper = MappedJdbcType.base[Value, String](_.toString, this.withName(_))

解决了我的问题。

于 2014-03-21T08:19:40.260 回答