2013-06-05 49 views
0

我有Netty Web服務器應該使用Hibernate檢索數據並將響應返回給客戶端。EventLoopGroup和BIO操作。

所以我只是想知道我的實現是正確的

ChannelPipeline pipeline = ch.pipeline(); 
     EventExecutorGroup ex = new DefaultEventExecutorGroup(64); 

     pipeline.addLast("decoder", new HttpRequestDecoder()); 
     pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); 
     pipeline.addLast("encoder", new HttpResponseEncoder()); 
     pipeline.addLast("chunkedWriter", new ChunkedWriteHandler()); 

     pipeline.addLast(ex,"handler", new HttpWebServerHandler()); 

,然後裏面messageReceived方法:

@Override 
    public void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request) 
      throws Exception { 
     if (!request.getDecoderResult().isSuccess()) { 
       sendError(ctx, BAD_REQUEST); 
       return; 
      } 
      ctx.executor().execute(new MyRunnable(ctx)); 
    } 

和裏面的Runnable:

@Override 
public void run() { 

     SomeObejct so = HibernateTemplate.getSomeObject() ; 
        String serializedSo = serialize(so); 
      FullHttpResponse res = new DefaultFullHttpResponse(
        HTTP_1_1, OK, Unpooled.copiedBuffer(serializedSo, CharsetUtil.UTF_8)); 
      res.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");      ctx.write(res).addListener(ChannelFutureListener.CLOSE);  
} 

我這樣做是正確的?

回答

1

是的,這看起來是正確的。您可能想要考慮的是從ChannelPipeline中刪除HttpObjectAggregator,因此可以節省一些內存開銷。但請注意,您需要由您自己處理不同的HTTP消息部分。