How to properly use delimiters?

Trustin Lee trustin at gmail.com
Mon Jan 3 22:03:41 EST 2011


Hi,

It depends on what delimiter you are going to use, but let me assume NUL (0x00) here.

You could simply prepend a DelimiterBasedFrameDecoder to your pipeline:

    pipeline.addLast(
        "frameDecoder", 
        new DelimiterBasedFrameDecoder(Delimiters.nulDelimiter()));

--
Trustin Lee - http://gleamynode.net/
Sent from a mobile device - please excuse the brevity.

On Dec 26, 2010, at 2:01 PM, pekalicious <pekalicious at gmail.com> wrote:

> 
> Hello JBoss community!
> 
> I am trying to create a basic client/server application that communicates
> with JSON. So far I have successfully created both the encoders and decoders
> for JSON <-> Message (a POJO class).
> 
> What I am trying to figure out is how can I guarantee that a JSON message
> isn't lost due to fragmentation. For example, I created a simple client
> where upon connection to the server it sends 10,000 Message's (which are
> converted into JSON). I noticed that many JSON messages are fragmented and
> thus getting an exception when trying to decode.
> 
> How should I proceed?
> 
> FYI the encoder is a SimpleChannelHandler:
> 
> 
>    Message msg = (Message) e.getMessage();
>        
>    byte[] bytes = PojoMapper.toJson(msg, false).getBytes();
>        
>    ChannelBuffer buffer = ChannelBuffers.buffer(bytes.length);
>    buffer.writeBytes(bytes);
>        
>    Channels.write(ctx, e.getFuture(), buffer);
> 
> 
> and the decoder is a StringDecoder
> 
> 
>    String json = (String) super.decode(ctx, channel, msg);
>        
>    System.out.println("Decoding json: " + json);
>        
>    Message message = PojoMapper.fromJson(json, Message.class);
>    return message;
> 
> 
> Thank you in advance!
> -- 
> View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/How-to-properly-use-delimiters-tp5867174p5867174.html
> Sent from the Netty User Group mailing list archive at Nabble.com.
> _______________________________________________
> netty-users mailing list
> netty-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-users



More information about the netty-users mailing list