首页 文章

使用Scala的Kryo的HazelcastSerializationException

提问于
浏览
1

我正在使用kryo与Scala 2.11.1和Hzaelcast 3.5进行序列化我试图将我的数据放入hazelcast Map 但我得到了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

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

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

这是我的User类的序列化器

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 中时,就像这样

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

Hcast Client 完成 StackTraces

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(用户)17:52:29.832 156859 [play-akka.actor.default-dispatcher-2] application ERROR - ! @ 6nb1hbglj - 内部服务器错误,(POST)[/ user / signup] - > play.api.Application $$ anon $ 1:执行异常[[HazelcastSerializationException:com.esotericsoftware.kryo.KryoException:无法创建类(非) static member class):scala.Enumeration $ Val序列化跟踪:user.Type(UserHistory)UserHistory(User)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.apply(PlayDefaultUpstreamHandler.scala:320)[play_2.11-2.3.8.jar:2.3.8] at play.core.server.netty.PlayDefaultUpstreamHandler $$ anonfun $ 3 $$ anonfun $ applyOrElse $ 4.apply (PlayDefaultUpstreamHandler.scala:320)[play_2.11-2.3.8.jar:2.3.8]

以下是来自 Hcast ServerStackTraces

17:52:28.010 [hz._hzInstance_1_dev.partition-operation.thread-0] INFO models.serializers.UserSerializer - **********读取字节并转换为用户对象17:52:28.990 [ hz._hzInstance_1_dev.partition-operation.thread-0]错误chmap.impl.operation.SetOperation - [192.168.15.20]:5701 [dev] [3.5] com.esotericsoftware.kryo.KryoException:无法创建类(非static member class):scala.Enumeration $ Val序列化跟踪:userType(UserHistory)UserHistory(User)com.hazelcast.nio.serialization.HazelcastSerializationException:com.esotericsoftware.kryo.KryoException:无法创建类(非静态成员类) :scala.Enumeration $ Val Serialization trace:userType(UserHistory)UserHistory(User)at com.hazelcast.nio.serialization.SerializationServiceImpl.handleException(SerializationServiceImpl.java:380)〜[hazelcast-3.5.jar:3.5] at com.hazelcast com中的.nio.serialization.SerializationServiceImpl.toObject(SerializationServiceImpl.java:282)〜[hazelcast-3.5.jar:3.5] . 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]在com.hazelcast.map的com.hazelcast.map.impl.mapstore.writethrough.WriteThroughStore.add(WriteThroughStore.java:31)〜[hazelcast-3.5.jar:3.5] .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]在com.hazelcast.spi.impl.operationservice.impl.OperationRunnerImpl.run(OperationRunnerImpl.java:137)〜[hazelcast-3.5.jar:3.5] 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 ]在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

    您是否注册了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)
    
  • 0

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

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

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

相关问题