使用Scala的Kryo的HazelcastSerializationException

时间:2015-09-01 13:32:40

标签: scala serialization hazelcast kryo

我正在使用kryo与Scala 2.11.1和Hzaelcast 3.5进行序列化 我试图将我的数据放入hazelcast地图,但我得到KryoException

这是我的模型类

  @SerialVersionUID(1)
        case class User( id : Int ,name : String, userType : UserType /*UserType is Enum (EMPLOYED , UNEMPLOYED)*/ , userhistory : UserHistory) extends  Serializable{

def this()= {
this(0,"",Active, null)
}
}

这里是UserHistory class

    @SerialVersionUID(1)
case  class UserHistory( date : DateTime = DateTime.now(), artworkStatus : ArtworkStatus = ACTIVE) extends  Serializable{

def this()={
    this(DateTime.parse("0"),ACTIVE) 
  }
}

这是我的用户类

的序列化程序
class UserSerializer extends StreamSerializer[User] {


   val log = LoggerFactory.getLogger(this.getClass)

   override def destroy() {

  }

  override def getTypeId() : Int ={
    val value : Int = 1;
    value

  }

  // takes the bytes and converts into User Object
   @throws(classOf[IOException])
  override def read(in : ObjectDataInput) : User =  {

     val kryo = new Kryo 
     val input = new Input(in.asInstanceOf[InputStream])
      log.info("********** Reading the bytes and converting into User object")
     kryo.readClassAndObject(input).asInstanceOf[User]



  }

   // takes User Object and converts into bytes
  @throws(classOf[IOException])
  override def write(out : ObjectDataOutput, obj : User) {

    val kryo= new Kryo
    val bops = new ByteArrayOutputStream
    val output = new Output(bops)
    kryo.writeClassAndObject(output , obj)
    output.flush
    output.close

    bops.writeTo(out.asInstanceOf[OutputStream])

    log.info("********** User object writen into bytes successfully")


  }


}

现在,当我将来自Hcast客户端的User类对象放入相应的地图时,就像这样

map.set(user.id , user)

它给了我这些例外:

 Cannot invoke the action, eventually got an error: com.hazelcast.nio.serialization.HazelcastSerializationException: com.esotericsoftware.kryo.KryoException: Class cannot be created (non-static member class): scala.Enumeration$Val

StackTraces完成Hcast Client

  

7:52:25.094 152121 [play-akka.actor.default-dispatcher-2]   UserSerializer INFO - **********用户对象写入字节   成功17:52:29.667 156694   [play-akka.actor.default-dispatcher-2]播放错误 - 无法调用   动作,最终得到一个错误:   com.hazelcast.nio.serialization.HazelcastSerializationException:   com.esotericsoftware.kryo.KryoException:无法创建类   (非静态成员类):scala.Enumeration $ Val序列化跟踪:   userType(UserHistory)   UserHistory(User)17:52:29.832   156859 [play-akka.actor.default-dispatcher-2]申请错误 -

     

! @ 6nb1hbglj - 内部服务器错误,(POST)   [/ user / signup] - >

     

play.api.Application $$ anon $ 1:执行   例外[HazelcastSerializationException:   com.esotericsoftware.kryo.KryoException:无法创建类   (非静态成员类):scala.Enumeration $ Val序列化跟踪:   userType(UserHistory)   UserHistory(用户)at   play.api.Application $ class.handleError(Application.scala:296)   〜[play_2.11-2.3.8.jar:2.3.8] at   play.api.DefaultApplication.handleError(Application.scala:402)   [play_2.11-2.3.8.jar:2.3.8] at   play.core.server.netty.PlayDefaultUpstreamHandler $$ anonfun $ 3 $$ anonfun $ applyOrElse $ 4.适用(PlayDefaultUpstreamHandler.scala:320)   [play_2.11-2.3.8.jar:2.3.8] at   play.core.server.netty.PlayDefaultUpstreamHandler $$ anonfun $ 3 $$ anonfun $ applyOrElse $ 4.适用(PlayDefaultUpstreamHandler.scala:320)   [play_2.11-2.3.8.jar:2.3.8]

以下是来自StackTraces

Hcast Server
  
    

17:52:28.010 [hz._hzInstance_1_dev.partition-operation.thread-0] INFO models.serializers.UserSerializer - **********读取字节     并转换为User对象17:52:28.990     [hz._hzInstance_1_dev.partition-operation.thread-0]错误     c.h.map.impl.operation.SetOperation - [192.168.15.20]:5701 [dev] [3.5]     com.esotericsoftware.kryo.KryoException:无法创建类     (非静态成员类):scala.Enumeration $ Val序列化跟踪:     userType(UserHistory)UserHistory(User)     com.hazelcast.nio.serialization.HazelcastSerializationException:     com.esotericsoftware.kryo.KryoException:无法创建类     (非静态成员类):scala.Enumeration $ Val序列化跟踪:     userType(UserHistory)UserHistory(User)at     com.hazelcast.nio.serialization.SerializationServiceImpl.handleException(SerializationServiceImpl.java:380)     〜[hazelcast-3.5.jar:3.5] at     com.hazelcast.nio.serialization.SerializationServiceImpl.toObject(SerializationServiceImpl.java:282)     〜[hazelcast-3.5.jar:3.5] at     com.hazelcast.map.impl.mapstore.AbstractMapDataStore.toObject(AbstractMapDataStore.java:78)     〜[hazelcast-3.5.jar:3.5] at     com.hazelcast.map.impl.mapstore.writethrough.WriteThroughStore.add(WriteThroughStore.java:39)     〜[hazelcast-3.5.jar:3.5] at     com.hazelcast.map.impl.mapstore.writethrough.WriteThroughStore.add(WriteThroughStore.java:31)     〜[hazelcast-3.5.jar:3.5] at     com.hazelcast.map.impl.DefaultRecordStore.set(DefaultRecordStore.java:803)     〜[hazelcast-3.5.jar:3.5] at     com.hazelcast.map.impl.operation.SetOperation.run(SetOperation.java:41)     〜[hazelcast-3.5.jar:3.5] at     com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.run(OperationRunnerImpl.java:137)     〜[hazelcast-3.5.jar:3.5] at     com.hazelcast.spi.impl.operationexecutor.classic.OperationThread.processOperation(OperationThread.java:154)     [hazelcast-3.5.jar:3.5] at     com.hazelcast.spi.impl.operationexecutor.classic.OperationThread.process(OperationThread.java:110)     [hazelcast-3.5.jar:3.5] at     com.hazelcast.spi.impl.operationexecutor.classic.OperationThread.doRun(OperationThread.java:101)     [hazelcast-3.5.jar:3.5] at     com.hazelcast.spi.impl.operationexecutor.classic.OperationThread.run(OperationThread.java:76)     [hazelcast-3.5.jar:3.5]引起:     com.esotericsoftware.kryo.KryoException:无法创建类     (非静态成员类):scala.Enumeration $ Val

  

请帮助我!!

2 个答案:

答案 0 :(得分:0)

您是否注册了github上提到的枚举序列化程序?

        // Serialization of Scala enumerations
        kryo.addDefaultSerializer(classOf[scala.Enumeration#Value], classOf[EnumerationSerializer])
        kryo.register(Class.forName("scala.Enumeration$Val"))
        kryo.register(classOf[scala.Enumeration#Value])

异常非常清楚,它无法创建枚举的userType

com.esotericsoftware.kryo.KryoException: Class cannot be created (non-static member class): scala.Enumeration$Val Serialization trace: userType (UserHistory) UserHistory (User)

答案 1 :(得分:0)

通常使用Scala,您应该使用StdInstantiatorStrategy,例如

val kryo = new Kryo
val is = new org.objenesis.strategy.StdInstantiatorStrategy
kryo.setInstantiatorStrategy(instantiatorStrategy)

不确定这是否会解决您的问题,但我还没有看到任何此类问题。