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

Karasko karas at uralweb.ru
Tue Apr 21 03:22:42 EDT 2009


Hi all. 
Thanks Trustin and other members of the team for Netty. Rather complicated
to start for newbie in Java and Networking,
but still promising framework for me.

Please help me with Netty server, which processes requests by doing various
SQL queries to MySql server. 
It should process as many clients as possible :)
I need some advice about it architecture and using of SQL connection and SQL
queries. I've tested it a little
with one client at one time, it works. 

How it works now:
1. DBMapper - A class responsive for DB creates a connection, an save it to
a static class field.
2. Server instance is created, and binds to the specific port.
3. I use such pipeline factory

Server.java

	MyHandlerObj handler = new ServerHandlerObj();
        bootstrap.setPipelineFactory(new ServerPipelineFactory(handler));


ServerPipelineFactory.java

   public ChannelPipeline getPipeline() throws Exception {
        ChannelPipeline pipeline = pipeline();
        pipeline.addLast("cmdDecoder", new MyCommandDecoder());
        pipeline.addLast("stringEncoder", new StringEncoder("UTF-8"));
        pipeline.addLast("handler", this.myhandler);
        return pipeline;
    } 


3. I use MyCommandDecoder which extends frameDecoder handler to return an
object specific to the protocol.

Protocol idea
Request: command code (byte), arguments string length (integer, 4 bytes),
string in bytes utf-8 encoding
Respose: string, two 0 bytes

If it is enough data in buffer, it should return a MyCommand object.

4. I use a handler to process business logic. It has an
@ChannelPipelineCoverage("all"), but i still don't understand
the whole meaning of this, but i know that annotation is only for warning
purposes.
In the handler - if e.getMessage is MyCommand instance, than i call
MyCommand.execute() method


	if (!(e.getMessage() instanceof MobileCmd)) {
            ctx.sendUpstream(e);
            return;
        }
        MobileCmd cmd = (MobileCmd)e.getMessage();
       
        String response = cmd.execute();
        ChannelFuture future = e.getChannel().write(response+"\00\00");

        if (cmd.isClose()) {
              future.addListener(ChannelFutureListener.CLOSE);
        }


Method MyCommand.execute() depending on request call some static method of
DBMapper class to do the
SQL queries with this class static connection field.

I can append all Java sources if it will help.

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




More information about the netty-users mailing list