Map [String,String]`别名的NotSerializableException [英] NotSerializableException for `Map[String, String]` alias
问题描述
我正在尝试将对象发送给远程角色,但出现了此异常:
I'm trying to send an object to a remote actor and I got this exception:
ERROR akka.remote.EndpointWriter - Transient association error (association remains live)
java.io.NotSerializableException: scala.collection.immutable.MapLike$$anon$2
要序列化的对象是一个case类:
The object being serialized is a case class:
case class LocationReport(idn: String, report: String, timestamp: Option[String], location: Attr, status: Attr, alarms: Attr, network: Attr, sensors: Attr) extends Message(idn) {
val ts = timestamp getOrElse location("fix_timestamp")
def json =
(report ->
("TIME" -> ts) ~
("location" -> location) ~
("alarms" -> alarms) ~
("network" -> network) ~
("sensors" -> ((status ++ sensors) + ("CUSTOMCLOCK" -> Report.decodeTimestamp(ts)))))
}
而 Attr
是类型r电子定义:
type Attr = Map[String, String]
Message
类非常简单:
abstract class Message(idn: String) {
def topic = idn
def json(): JValue
}
我想知道别名/重新定义类型是否会混淆序列化程序。我想我正在使用ProtoBuf序列化,但是在堆栈跟踪中确实看到了 JavaSerializer
。
I'm wondering if the type alias/redefinition is confusing the serializer. I think I'm using ProtoBuf serialization, but I do see JavaSerializer
in the stacktrace.
更多调试信息
我新建了一个JavaSerializer,并分别序列化了每个Map。只有一个(警报
)无法序列化。这是它们每个的toString:
I newed up a JavaSerializer and individually serialized each of the Maps. Only one (alarms
) fails to serialize. Here's the toString of each of them:
此失败:
alarms = Map(LOWBATTERY -> 1373623446000)
这些成功了:
location = Map(a_value -> 6, latitude -> 37.63473, p_value -> 4, longitude -> -97.41459, fix_timestamp -> 3F0AE7FF, status -> OK, fix_type -> MSBL, CUSTOMCLOCK -> 1373644159000)
network = Map(SID -> 1271, RSSI -> 85)
sensors = Map(HUMIDITY -> -999, PRESSURE -> -999, LIGHT -> -999 9:52 AM)
status = Map(TEMPERATURE_F -> 923, CYCLE -> 4, TEMPERATURE1_C -> 335, CAP_REMAINING -> 560, VOLTAGE -> 3691, CAP_FULL -> 3897)
推荐答案
问题是 Map.mapValues
生成了不可序列化的对象。创建警报后,它会通过 alarms.mapValues(hex2Int)
之类的内容运行。问题和解决方法在此处描述:
The problem is that Map.mapValues
produces an object that's not serializable. When alarms was created, it's run through something like alarms.mapValues(hex2Int)
. The problem and workaround is described here:
https://issues.scala-lang.org/browse/SI-7005
总之,解决方案是执行 alarms.mapValues(hex2Int).map(identity)
In short, the solution is to do alarms.mapValues(hex2Int).map(identity)
这篇关于Map [String,String]`别名的NotSerializableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!