Netty中的多个处理程序 [英] Multiple handlers in netty

查看:132
本文介绍了Netty中的多个处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的代码现在看起来像这样: 可以在 http://上找到HexDumpProxyInboundHandler 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).

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

I would like to make a kind of logging proxy in netty. The goal is to be able to have a web browser make HTTP requests to a netty server, have them be passed on to a back-end web server, but also be able to take certain actions based on HTTP specific things.

There's a couple of useful netty exmaples, HexDumpProxy (which does the proxying part, agnostic to the protocol), and I've taken just a bit of code from HttpSnoopServerHandler.

My code looks like this right now: HexDumpProxyInboundHandler can be found at 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
  }
}

Unfortunately messageReceived in HttpSnoopServerHandler never gets called - it seems like HexDumpProxyInboundHandler consumes all the events.

How can I have two handlers, where one of them requires a decoder but the other doesn't (I'd rather have HexDumpProxy as it is, where it doesn't need to understand HTTP, it just proxies all connections, but my HttpSnoopHandler needs to have HttpRequestDecoder in front of it)?

解决方案

I've not tried it but you could extend HexDumpProxyInboundHandler and override messageReceived with something like

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

Alternatively you could modify HexDumpProxyInboundHandler directly to that the last thing messageReceived does is call super.messageReceived(ctx,e).

This would only work for inbound data from the client. Data from the service you're proxy-ing would still be passed through without you code seeing it.

这篇关于Netty中的多个处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆