Map [String,String]`别名的NotSerializableException

编程入门 行业动态 更新时间:2024-10-28 10:33:17
本文介绍了Map [String,String]`别名的NotSerializableException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将对象发送给远程角色,但出现了此异常:

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:

issues.scala-lang/browse/SI-7005

总之,解决方案是执行 alarms.mapValues(hex2Int).map(identity)

In short, the solution is to do alarms.mapValues(hex2Int).map(identity)

更多推荐

Map [String,String]`别名的NotSerializableException

本文发布于:2023-07-15 18:12:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1112875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:别名   Map   String   NotSerializableException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!