简体   繁体   English

Netty编码/解码Java对象

[英]Netty Encoding/Decoding Java Object

I am using Netty 3.9. 我正在使用Netty 3.9。 I have a simple client server setup which I got from http://en.wikipedia.org/wiki/Netty_%28software%29#Netty_TCP_Example . 我有一个简单的客户端服务器设置,可从http://en.wikipedia.org/wiki/Netty_%28software%29#Netty_TCP_Example获得 I have expanded the example to send a Java search plan object from the client to the server 我已经扩展了示例,以将Java搜索计划对象从客户端发送到服务器

The search plan object is a 3rd party object which has methods for serializing and deserializing. 搜索计划对象是第三方对象,具有用于序列化和反序列化的方法。 Serialization writes the object into a byte[] array. 序列化将对象写入byte []数组。 My client pipeline factory looks like this: 我的客户管道工厂看起来像这样:

this.bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
        @Override
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(
                    new StringDecoder(CharsetUtil.UTF_8),
                    new StringEncoder(CharsetUtil.UTF_8),
                    new DelimiterBasedFrameDecoder(
                            ALLOWED_CHARACTER_BUFFER_SIZE, Delimiters
                                    .lineDelimiter()),

                    /* We also add our Clients own ChannelHandler. */
                    new ClientChannelHandler());
        }
    });

I think StringDecoder and StringEncoder are incorrect. 我认为StringDecoder和StringEncoder不正确。 I think I need some sort of ByteEncoder/Decoder which I do not see. 我想我需要某种我看不到的ByteEncoder / Decoder。 Do I need to write these? 我需要写这些吗? I tried this code to convert to a String on the Client 我尝试将此代码转换为客户端上的字符串

 byte[] byteVersion = searchPlanRepo.serialize(missionNum);  // serialize the search plan
 searchPlanStr = new String(byteVersion, StandardCharsets.UTF_8);

but on the server no matter what I do to "deserialize" the object I fail. 但是在服务器上无论我如何“反序列化”我失败的对象。 I continuously get the error message: 我不断收到错误消息:

"java.lang.ClassCastException: java.lang.String cannot be cast to payload.mission.SearchPlanType" “ java.lang.ClassCastException:java.lang.String无法转换为payload.mission.SearchPlanType”

My questions: 我的问题:

  1. Do I need a custom byte encoder/decoder? 是否需要自定义字节编码器/解码器? Are there any examples? 有没有例子?
  2. Serialization seems simple: byte array to String but deserialization from a String to a byte array does not work. 序列化似乎很简单:从字节数组到字符串,但是从字符串反序列化到字节数组不起作用。 I am sure I am missing something. 我确定我缺少什么。 Can anyone point me in the correct direction? 谁能指出我正确的方向?

Thanks for taking the time to read this. 感谢您抽时间阅读。 :) :)

Phil 菲尔

You might have a look to the serialization codec: 您可能会看一下序列化编解码器:

http://netty.io/3.9/api/org/jboss/netty/handler/codec/serialization/package-summary.html http://netty.io/3.9/api/org/jboss/netty/handler/codec/serialization/package-summary.html

As to the associated example: 至于相关的例子:

https://github.com/netty/netty/tree/3.9/src/main/java/org/jboss/netty/example/objectecho https://github.com/netty/netty/tree/3.9/src/main/java/org/jboss/netty/example/objectecho

They illustrate how you can serialize/deserialize objects. 它们说明了如何序列化/反序列化对象。 Of course you can also write your own codec if it is more specific. 当然,如果更具体,您也可以编写自己的编解码器。

I looked at the sample ObjectDecoder and ObjectEncoder examples and this solved my problem. 我查看了示例ObjectDecoder和ObjectEncoder示例,这解决了我的问题。 I changed the pipeline code on the client and I can now send out my missions. 我在客户端上更改了流水线代码,现在可以发出任务了。 Here is the code from the client/sender: 这是来自客户端/发送者的代码:

this.bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

        @Override
        public ChannelPipeline getPipeline() throws Exception {
            return Channels.pipeline(
                    new ObjectEncoder(),
                    new ObjectDecoder(ClassResolvers
                            .cacheDisabled(getClass().getClassLoader())),
                    new ClientChannelHandler());
        }
    });

The pipelineFactory code on the server/receiver is nearly identical. 服务器/接收器上的pipelineFactory代码几乎相同。 The only difference is the handler for each side. 唯一的区别是每一侧的处理程序。 The client uses ClientChannelHandler which simply logs a message from the Server. 客户端使用ClientChannelHandler,它仅记录来自服务器的消息。 The server uses a ServerChannelHandler which takes the received mission and casts it to the correct Java type so I can manipulate it. 服务器使用ServerChannelHandler,它执行接收到的任务并将其强制转换为正确的Java类型,因此我可以对其进行操作。

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

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