Question

I'm currently digging into Apache MINA. It's a great framework with lots of capabilities. The hardest part until now was the decoder part. Checking the api documents I understand that there are the following classes that one can extend and implement his own:

  1. DemuxingProtocolDecoder - A composite ProtocolDecoder that demultiplexes incoming IoBuffer decoding requests into an appropriate MessageDecoder.
  2. ObjectSerializationDecoder - A ProtocolDecoder which deserializes Serializable Java objects using IoBuffer.getObject(ClassLoader).
  3. PrefixedStringDecoder - A ProtocolDecoder which decodes a String using a fixed-length length prefix.

All of the above extend the CumulativeProtocolDecoder class - A ProtocolDecoder that cumulates the content of received buffers to a cumulative buffer to help users implement decoders.

  • Could you please mention with some real world examples what subclass of CumulativeProtocolDecoder you would or did use and why?
  • Is there an example that doesn't need the decoder to extend the CumulativeProtocolDecoder class and just implement the ProtocolDecoder directly without worrying about fragmentation?
Was it helpful?

Solution

I am using an instance of DemuxingProtocolDecoder class with my application. Under the package org.apache.mina.filter.codec.demux there are some interfaces and classes that you can use to decode your messages. There is an interface called MessageDecoder. Create your own class that implements this interface and MINA will the work. Something like this,

public class MyDecoder implements MessageDecoder {
      public MessageDecoderResult decode(IoSession session, IoBuffer buffer, ProtocolDecoderOutput decoderOutput) throws Exception {
           /* Your
              decode
              mechanism */
           decoderOutput.write(message); // don't forget to write your decoded message object at some point.
           return MessageDecoder.OK; //or something else that matches your needs.
      }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top