Netty server with MySql database backend, how to do it right?

Frederic Bregier fredbregier at free.fr
Thu Apr 23 09:50:40 EDT 2009


Hi Karasko,

Yes I forgot that point. You're right. You should need to add an
OrderedMemoryAwareThreadPoolExecutor since SQL connection (and moreover
requests) can take long time and so locking the IO threads (which is always
a bad idea). 

You need such executor if what you're going to do with the message
can take long time or can be blocking (like sql requests).
If not (short time, not blocking), then it is perfectly ok to not insert
such an executor.

The prefered position in the pipeline should be something similar to this:
Pipeline:
- first, all codec (since they only make quick computations and no blocking
actions)
- then the executor (OrderedMemoryAwareThreadPoolExecutor)
- then finally any handler (at least the business one, the one that do the
real stuff
  like your sql connections and answers)

To use it (if I suppose correctly that each client should have its own
session
so that the business handler should be unique for each channel/client),
you have to use a ChannelPipelineFactory and do something like:

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

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

and when you create your server:

  ServerBootstrap bootstrap = new ServerBootstrap(factory);
  OrderedMemoryAwareThreadPoolExecutor globalExecutor =
     new OrderedMemoryAwareThreadPoolExecutor(...);
  // for instance (200,0,0) will allow 200 concurrent threads with no memory
  // limitation. 200 concurrent threads will correspond for you to a maximum
  // of 200 active and running sql connections
  MyPipelineFactory myPipelineFactory = new
MyPipelineFactory(globalExecutor);
  bootstrap.setPipelineFactory(myPipelineFactory); 

Take also a look a the examples of Netty of how to shutdown the server in
order to
correctly shutdown the executor and all connections.
And for your server, I would suggest to take care when it shutdowns about 
all sql connections still opened in the pool, in order to close
them in a correct way (DBCP should have a function to clean or shutdown
all pooled sql connections). Sql database do not like to see "brutal"
disconnection... 
;-)

HTH,
Frederic



-----
Hardware/Software Architect
-- 
View this message in context: http://n2.nabble.com/Netty-server-with-MySql-database-backend%2C-how-to-do-it-right--tp2668426p2683324.html
Sent from the Netty User Group mailing list archive at Nabble.com.




More information about the netty-users mailing list