简体   繁体   中英

Netty Encoding/Decoding Java Object

I am using 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 . I have expanded the example to send a Java search plan object from the client to the server

The search plan object is a 3rd party object which has methods for serializing and deserializing. Serialization writes the object into a byte[] array. 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. I think I need some sort of ByteEncoder/Decoder which I do not see. 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"

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

As to the associated example:

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. 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. The only difference is the handler for each side. The client uses ClientChannelHandler which simply logs a message from the Server. The server uses a ServerChannelHandler which takes the received mission and casts it to the correct Java type so I can manipulate it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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