netty-server with RoutingPipeline

gaerfield gaerfield at web.de
Mon Mar 9 18:32:56 EDT 2009


Provides netty a standard-way to implement a router? I thought for smth like
the following case:

public class RouterPipelineFactory implements ChannelPipelineFactory {
    public ChannelPipeline getPipeline() throws Exception {
        ChannelPipeline pipeline = Channels.pipeline();
        pipelineAddFirst("router", new Router());
        // several ways a message could pass
        pipeline.addChild("1", new Unzipper());
        pipeline.addChild("2", new DefaultObjectDecoding());
        pipeline.addChild("3", new RawData());
    }
}

public class Router extends SimpleChannelHandler {
    @Override
    public void messageReceived(
            ChannelHandlerContext ctx, MessageEvent e) {
        // reading a few bytes
        ChannelBuffer buf = (ChannelBuffer) e.getMessage();
        if (buf.readableBytes() >= 4) {
            int messageType = buf.readInt();
            switch (messageType) {
                case 1:
                    e.getChannel.getChild("1").messageReceived(e);
                    break;
                case 2:
                    e.getChannel.getChild("2").messageReceived(e);
                    break;
                case 3:
                    e.getChannel.getChild("3").messageReceived(e);
                    break;
                default:
                    drop();
            }
        }
    }
}

The content-type of the Message (defined in some kind of header) then
determines how message gets to its endpoint or passes several optional pipes
on its way (maybe not all messages are zipped).

Anyway, netty is a really impressive and easy usable framework with
amazingly good documentation, great thanks for that.
-- 
View this message in context: http://n2.nabble.com/netty-server-with-RoutingPipeline-tp2452056p2452056.html
Sent from the Netty User Group mailing list archive at Nabble.com.




More information about the netty-users mailing list