Kyro向我发出以下信息:
com.esotericsoftware.kryo.KryoException: Encountered unregistered class ID: 44
at com.esotericsoftware.kryo.util.DefaultClassResolver.readClass(DefaultClassResolver.java:137)
at com.esotericsoftware.kryo.Kryo.readClass(Kryo.java:693)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:804)
at main.java.com.bag.client.TestClient.processReadReturn(TestClient.java:246)
at main.java.com.bag.client.TestClient.replyReceived(TestClient.java:221)
at bftsmart.communication.client.netty.NettyClientServerCommunicationSystemClientSide.channelRead0(NettyClientServerCommunicationSystemClientSide.java:244)
at bftsmart.communication.client.netty.NettyClientServerCommunicationSystemClientSide.channelRead0(NettyClientServerCommunicationSystemClientSide.java:65)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:292)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:278)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:277)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:264)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:292)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:278)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:962)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:131)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:528)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:485)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:399)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:371)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:112)
at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
at java.lang.Thread.run(Thread.java:745)
我使用以下代码段进行序列化:
KryoPool pool = new KryoPool.Builder(factory).softReferences().build();
Kryo kryo = pool.borrow();
Input input = new Input(bytes);
ArrayList<NodeStorage> nodeStorage = new ArrayList<>();
ArrayList<RelationshipStorage> relationshipStorage = new ArrayList<>();
kryo.writeClassAndObject(output, nodeStorage);
kryo.writeClassAndObject(output, relationshipStorage);
工厂:
private KryoFactory factory = () ->
{
Kryo kryo = new Kryo();
kryo.register(NodeStorage.class, 100);
kryo.register(RelationshipStorage.class, 200);
// configure kryo instance, customize settings
return kryo;
};
使用以下内容进行反序列化:
KryoPool pool = new KryoPool.Builder(factory).softReferences().build();
Kryo kryo = pool.borrow();
ArrayList<NodeStorage> nodeResult = (ArrayList<NodeStorage>) kryo.readClassAndObject(input);
ArrayList<RelationshipStorage> relationshipResult = (ArrayList<RelationshipStorage>) kryo.readClassAndObject(input);
崩溃日志在执行第一个arrayList反序列化时出现。
NodeStorage和Relationship存储包含各种字符串,int和带有更多字符串的映射。
完整的代码可以在这里找到:
答案 0 :(得分:0)
我认为你需要一个新的序列化器。
public class ArraySerializer extends CollectionSerializer{
protected Collection create (Kryo kryo, Input input, Class<Collection> type) {
return new ArrayList();
}
}
kryo.register(Arrays.asList().getClass(), new ArraySerializer());