使用函数的结果作为新函数的参数,但是为什么会发生异常?

时间:2018-06-20 01:29:30

标签: java kryo

我写了一个精简的RPC框架,并使用kryo序列化对象。 但是当我这样写的时候发生了异常

RpcResponse rpcResponse = new RpcResponse(input.readInt(), kryo.readClassAndObject(input), input.readBoolean());

所以我使用了另一种方法来初始化我的RpcResponse对象。

int id = input.readInt();
boolean isInvokeSuccess = input.readBoolean();
Object resultOrThrowable = kryo.readClassAndObject(input);

RpcResponse rpcResponse = new RpcResponse(id, resultOrThrowable, isInvokeSuccess);

它有效。

所以我的问题是:我使用的这两种方式之间有什么区别吗?

例外是

io.netty.handler.codec.DecoderException: com.esotericsoftware.kryo.KryoException: Unable to find class: abc123123
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:459)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:265)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:647)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:582)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:499)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:461)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.esotericsoftware.kryo.KryoException: Unable to find class: abc123123
at com.esotericsoftware.kryo.util.DefaultClassResolver.readName(DefaultClassResolver.java:156)
at com.esotericsoftware.kryo.util.DefaultClassResolver.readClass(DefaultClassResolver.java:133)
at com.esotericsoftware.kryo.Kryo.readClass(Kryo.java:670)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:781)
at org.develcarl.rpc.serializer.RpcResponseSerializer.read(RpcResponseSerializer.java:30)
at org.develcarl.rpc.serializer.RpcResponseSerializer.read(RpcResponseSerializer.java:14)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:790)
at org.develcarl.rpc.serializer.KryoSerializer.deserialize(KryoSerializer.java:56)
at org.develcarl.rpc.netty.NettyKryoDecoder.decode(NettyKryoDecoder.java:28)
at io.netty.handler.codec.LengthFieldBasedFrameDecoder.decode(LengthFieldBasedFrameDecoder.java:343)
at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:489)
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:428)
... 16 more
Caused by: java.lang.ClassNotFoundException: abc123123
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at com.esotericsoftware.kryo.util.DefaultClassResolver.readName(DefaultClassResolver.java:154)
... 27 more

1 个答案:

答案 0 :(得分:1)

是的。第二个作品。操作顺序事项,您有intbooleanObject。在第一个示例中,您阅读了intObjectboolean。你可以的,

int id = input.readInt();
boolean isInvokeSuccess = input.readBoolean();
RpcResponse rpcResponse = new RpcResponse(id, 
        kryo.readClassAndObject(input), isInvokeSuccess);

无论如何,您都必须按照字段的写入顺序对其进行读取。