【发布时间】:2012-04-18 16:10:01
【问题描述】:
我想在 netty 中做一种日志代理。目标是能够让 Web 浏览器向 netty 服务器发出 HTTP 请求,将它们传递到后端 Web 服务器,而且还能够基于 HTTP 特定的事物采取某些行动。
有几个有用的网络示例,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)?
【问题讨论】: