2013-07-17 22 views
6

我試圖對象發送到遠程的演員,我得到這個異常:NotSerializableException爲`地圖[字符串,字符串]`別名

ERROR akka.remote.EndpointWriter - Transient association error (association remains live) 
java.io.NotSerializableException: scala.collection.immutable.MapLike$$anon$2 

正在連載的對象是一個案例類:

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是一種重新定義:

type Attr = Map[String, String] 

Message類很簡單:

abstract class Message(idn: String) { 
    def topic = idn 
    def json(): JValue 
} 

我想知道如果類型別名/重定義是令人困惑的序列化程序。我想我正在使用ProtoBuf序列化,但我確實在堆棧跟蹤中看到了JavaSerializer

更多的調試信息

我newed了JavaSerializer和單獨序列每個地圖。只有一個(alarms)無法序列化。下面是他們每個人的的toString:

這一次失敗:

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) 
+3

類型別名與此完全無關。這是一個運行時問題,類型別名僅在編譯時存在。 – ghik

+0

如果您尚未爲您的類定義protobuf協議並正在使用這些協議,那麼您並未使用ProtobufSerialization。 –

+0

@ViktorKlang我將其更改爲JavaSerialization,它仍然無法正常工作。我認爲它與序列化Map相關,正如@ghik指出的那樣,這不是由別名引起的。有任何想法嗎? – kelloti

回答

26

的問題是,Map.mapValues產生的對象,這不是序列化。當警報被創建時,它通過類似alarms.mapValues(hex2Int)的運行。存在的問題及解決方法,這裏描述:

https://issues.scala-lang.org/browse/SI-7005

總之,解決的辦法是做alarms.mapValues(hex2Int).map(identity)

0

不知道這是否適用於所有情況,但我的解決方法是簡單地在地圖轉換成序列(序列前只是.toSeq)。在反序列化之後,toMap應該給你相同的映射。

相關問題