繁体   English   中英

Netty中的多个处理程序

[英]Multiple handlers in netty

我想在netty中创建一种日志记录代理。 目标是能够使Web浏览器向Netty服务器发出HTTP请求,将其传递到后端Web服务器,而且还能够基于HTTP特定的事物执行某些操作。

有两个有用的Netty示例,即HexDumpProxy(完成代理部分,与协议无关),而我仅从HttpSnoopServerHandler中获取了一些代码。

我的代码现在看起来像这样:HexDumpProxyInboundHandler可以在http://docs.jboss.org/netty/3.2/xref/org/jboss/netty/example/proxy/HexDumpProxyInboundHandler.html中找到

//in HexDumpProxyPipelineFactory
  public ChannelPipeline getPipeline() throws Exception {
      ChannelPipeline p = pipeline(); // Note the static import.
      p.addLast("handler", new HexDumpProxyInboundHandler(cf, remoteHost, remotePort));

      p.addLast("decoder", new HttpRequestDecoder());
      p.addLast("handler2", new HttpSnoopServerHandler());
      return p;
  }


//HttpSnoopServerHandler
public class HttpSnoopServerHandler extends SimpleChannelUpstreamHandler {
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
    HttpRequest request = (HttpRequest) e.getMessage();
    System.out.println(request.getUri());
    //going to do things based on the URI
  }
}

不幸的是,HttpSnoopServerHandler中接收到的messageReceived从未被调用过-好像HexDumpProxyInboundHandler消耗了所有事件。

我怎么能有两个处理程序,其中一个需要解码器,而另一个则不需要(我宁愿拥有HexDumpProxy,它不需要理解HTTP,它只是代理所有连接,但是我的HttpSnoopHandler需要在其前面有HttpRequestDecoder)?

我没有尝试过,但是您可以扩展HexDumpProxyInboundHandler并使用类似以下内容覆盖messageReceived

super.messageReceived(ctx, e);
ctx.sendUpstream(e);

或者,您可以直接将HexDumpProxyInboundHandler修改为messageReceived所做的最后一件事是调用super.messageReceived(ctx,e)。

这仅适用于来自客户端的入站数据。 您正在代理的服务中的数据仍会传递,而您无需代码即可看到它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM