繁体   English   中英

使用Scala的Kryo的HazelcastSerializationException

[英]HazelcastSerializationException with Kryo using Scala

我正在使用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

    @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.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(User)17:52:29.832 156859 [play-akka.actor.default-dispatcher-2]应用程序错误-

@ 6nb1hbglj-内部服务器错误,用于(POST)[/用户/注册]->

play.api.Application $$ anon $ 1:执行例外[[HazelcastSerializationException:com.esotericsoftware.kryo.KryoException:无法创建类(非静态成员类):scala.Enumeration $ Val序列化跟踪:userType(UserHistory)UserHistory(用户)在play.api.Application $ class.handleError(Application.scala:296)〜[play_2.11-2.3.8.jar:2.3.8]在play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.8.jar:2.3.8]在play.core.server.netty.PlayDefaultUpstreamHandler $$ anonfun $ 3 $$ anonfun $ applyOrElse $ 4.apply(PlayDefaultUpstreamHandler.scala:320)[play_2.11-2.3 .8.jar:2.3.8]在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-**********读取字节并将其转换为User对象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:无法创建类(非静态成员class):scala.Enumeration $ Val序列化跟踪:userType(UserHistory)UserHistory(User)com.hazelcast.nio.serialization.HazelcastSerializationException:com.esotericsoftware.kryo.KryoException:无法创建类(非静态成员类):scala .Enumeration $ Val序列化跟踪:com.hazelcast.nio.serialization.SerializationServiceImpl.handleException(SerializationServiceImpl.java:380)上的userType(UserHistory)UserHistory(User)〜com.hazelcast.nio上的[hazelcast-3.5.jar:3.5] .serialization.SerializationServiceImpl.toObject(SerializationServiceImpl.java:282)〜[hazelcast-3.5.jar:3.5]在com.haze lcast.map.impl.mapstore.AbstractMapDataStore.toObject(AbstractMapDataStore.java:78)〜[hazelcast-3.5.jar:3.5]位于com.hazelcast.map.impl.mapstore.writethrough.WriteThroughStore.add(WriteThroughStore.java:39 )〜[hazelcast-3.5.jar:3.5]在com.hazelcast.map.impl.mapstore.writethrough.WriteThroughStore.add(WriteThroughStore.java:31)〜[hazelcast-3.5.jar:3.5]在com.hazelcast.map .impl.DefaultRecordStore.set(DefaultRecordStore.java:803)〜[hazelcast-3.5.jar:3.5]在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。 com.hazelcast.spi.impl.operationexecutor.classic.OperationThread.process(OperationThread.java:110)上的OperationThread.processOperation(OperationThread.java:154)[hazelcast-3.5.jar:3.5] [hazelcast-3.5.jar:3.5 ] com.hazelcast.spi.impl.operationexecutor.classic.Oper ationThread.doRun(OperationThread.java:101)[hazelcast-3.5.jar:3.5]位于com.hazelcast.spi.impl.operationexecutor.classic.OperationThread.run(OperationThread.java:76)[hazelcast-3.5.jar:3.5 ]原因:com.esotericsoftware.kryo.KryoException:无法创建类(非静态成员类):scala.Enumeration $ Val

请帮我!!

您是否按照github所述注册了enum序列化程序?

        // 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)

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

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

不知道这是否可以解决您的问题,但是我还没有看到任何此类问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM