Structured message reading problem in Netty.

Frederic Bregier fredbregier at free.fr
Thu Apr 23 09:28:10 EDT 2009


Hi Manish,

First, to follow your comment on the wiki, the ping pong example is based on
the 3.1 release. I tried to answer you directly from the wiki, but I can't
(neither with Firefox neither with IE). Don't know why...
So the channelGroup and other codes are refering the 3.1 version, not the
3.0.

I take a look at the code example you give attached. You use one unique
handler
for all client:

  ServerBootstrap bootstrap = new ServerBootstrap(factory);
  DiscardServerHandler handler = new DiscardServerHandler();
  ChannelPipeline pipeline = bootstrap.getPipeline();
  pipeline.addLast("handler", handler);

So that if you have only one client, it is ok, but if you have many clients
at the same time, then they will all shared the exactly same handler and so
its attribute.

And as your handler is a statefull handler (almost an ObjectDecoder as much
as
I can see), you will have problem since two channels (two clients) will
share
the exactly same handler and so its private variable.
You have to make this handler new for each channel.

So what I suggest to you is the following:

1) Make your server by using a pipelineFactory like:
  ServerBootstrap bootstrap = new ServerBootstrap(factory);
  MyPipelineFactory myPipelineFactory = new MyPipelineFactory();
  bootstrap.setPipelineFactory(myPipelineFactory);

See the PingPong example if you want from the wiki, but there are also
examples in the standard distribution of Netty for this.

The MyPipelineFactory could be something like:

public class MyPipelineFactory implements ChannelPipelineFactory {
  public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = Channels.pipeline();
    DiscardServerHandler handler = new DiscardServerHandler();
    pipeline.addLast("handler", handler);
    return pipeline;
  }
}

2) I would suggest you to make one codec (in fact almost your
DiscardServerHandler
class) and another one for your business (let say MyBusinessHandler) for the
real stuff (what you have to do like writes back an answer, computes
something).

So you MyPipelineFactory could become:

  public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = Channels.pipeline();
    DiscardServerHandler handler = new DiscardServerHandler();
    pipeline.addLast("handler", handler);
    MyBusinessHandler business = new MyBusinessHandler();
    pipeline.addLast("business", business);
    return pipeline;
  }

3) If your computations could lead to big computations or waiting operations
(like IO on files or database), I suggest you to insert between the two
handlers
(the codec one and the business one) one
OrderedMemoryAwareThreadPoolExecutor
in order to not block the IO threads, like:

public class MyPipelineFactory implements ChannelPipelineFactory {
  OrderedMemoryAwareThreadPoolExecutor globalExecutor;
  public MyPipelineFactory(OrderedMemoryAwareThreadPoolExecutor
globalExecutor) {
    this.globalExecutor = globalExecutor;
  }

  public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = Channels.pipeline();
    DiscardServerHandler handler = new DiscardServerHandler();
    pipeline.addLast("handler", handler);
    pipeline.addLast("executor", globalExecutor);
    MyBusinessHandler business = new MyBusinessHandler();
    pipeline.addLast("business", business);
    return pipeline;
  }

4) I would suggest you to look at the FrameDecoder since your data
organization
seems more or less to fit better with it, perhaps even
LengthFieldBasedFrameDecoder
should just answer to your needs.

Hope this helps you,

Frederic



-----
Hardware/Software Architect
-- 
View this message in context: http://n2.nabble.com/Structured-message-reading-problem-in-Netty.-tp2682454p2683186.html
Sent from the Netty User Group mailing list archive at Nabble.com.




More information about the netty-users mailing list