gpt4 book ai didi

scala - Akka持久化枚举的NotSerializableException

转载 作者:行者123 更新时间:2023-12-01 17:32:57 25 4
gpt4 key购买 nike

我正在尝试使用 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)

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

最佳答案

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

我正在使用 slick 与我的数据库通信,所以我声明了非常有用的 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(_))

解决了我的问题。

关于scala - Akka持久化枚举的NotSerializableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22535125/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com