netty学习1

思想:借助netty的NIO功能,并接合servlet或者restlet来构建一个web框架

http://www.jroller.com/agoubard/entry/run_servlets_with_netty#.UiYEBtL8lCx

Run Servlets with Netty

Java Servlets have been vastly used in companies for more than 10 years now. Recently another project from JBoss named Netty has gained on popularity to serve data. From Netty website: "Netty has succeeded to find a way to achieve ease of development, performance, stability, and flexibility without a compromise.".

But at the moment to serve data or to handle requests, you need to choose for the implementation either for a Servlet or for Netty.

As main developer of XINS, an open-source web services framework, I've developed a few years ago a basic Servlet container to be able to run unit tests and to run WAR if you wanted to withjava -jar my-api.war or xins run-my-api.

For the release of XINS 3.0, I've decided to put the Servlet container on top of Netty.
Netty takes cares of the IO and the HTTP handling and my Servlet container takes care of the handling of the HTTP request to the Servlet itself.

How to do it

First you need to handle HTTP data with a Netty  ChannelPipeline  using a  pipeline factory :
public class DefaultNettyServletPipelineFactory implements ChannelPipelineFactory {
...
public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = pipeline();

    pipeline.addLast("decoder", new HttpRequestDecoder());
    pipeline.addLast("encoder", new HttpResponseEncoder());
    pipeline.addLast("deflater", new HttpContentCompressor());
    pipeline.addLast("handler", servletHandler); // will convert http request to servlet request
    return pipeline;
}

Then you will need a NettyServletHandler that converts Netty HttpRequest to a Servlet request:

public class NettyServletHandler extends SimpleChannelUpstreamHandler {
...
@Override
public void messageReceived(ChannelHandlerContext context, MessageEvent event) throws Exception {
    HttpRequest request = (HttpRequest) event.getMessage();
    // Then get URL, method, headers, ... and pass the values to the Servlet container.
}

You will also need a method to start the server:

   public void startServer(int port, String pipelineFactory) throws Exception {
      ServerBootstrap server = new ServerBootstrap(
              new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));

      if (pipelineFactory == null) { // If the user doesn't have a specific pipeline use the default one
         pipelineFactory = "org.xins.common.servlet.container.DefaultNettyServletPipelineFactory";
      }
      DefaultNettyServletPipelineFactory pipelineFactoryClass = (DefaultNettyServletPipelineFactory) Class.forName(pipelineFactory).newInstance();
      pipelineFactoryClass.setServletHandler(this);

      server.setPipelineFactory(pipelineFactoryClass);

      server.bind(new InetSocketAddress(port));
   }

And voilà! Your server code has the best of both worlds: it uses a standard API and it runs on Netty.

( Sep 27 2012, 07:00:00 AM CEST )   Permalink

COMMENTS:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值