channel.write中的NPE(对象消息)

时间:2012-11-30 13:03:38

标签: netty

我正在运行基于netty(3.2.5.Final.jar)的(自制)中间件“服务连接器(SC)”,它基本上是http流量的代理。任何传入请求都会转发到远程节点,最后是apache。

public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception {
        ChannelBuffer msg = (ChannelBuffer) event.getMessage();
        outboundChannel.write(msg);
}

我通过浏览器向SC发送请求,例如:“http:// localhost:9104 / testHtml.htm”

它通常很完美!浏览器偶尔会永远循环。不同的模式(不同的文件,大小,时间)

查看netty日志:

2012-11-28 17:54:12.326+0100 [New I/O server boss #9 ([id: 0x015bdc50, /10.43.18.160:9104])] DEBUG (org.jboss.netty.handler.logging.LoggingHandler:39) - [id: 0x00f6fd54, /10.43.18.160:52499 => /10.43.18.160:9104] OPEN
2012-11-28 17:54:12.342+0100 [New I/O server boss #9 ([id: 0x015bdc50, /10.43.18.160:9104])] DEBUG (org.jboss.netty.handler.logging.LoggingHandler:39) - [id: 0x00f6fd54, /10.43.18.160:52499 => /10.43.18.160:9104] BOUND: /10.43.18.160:9104
2012-11-28 17:54:12.357+0100 [New I/O server boss #9 ([id: 0x015bdc50, /10.43.18.160:9104])] DEBUG (org.jboss.netty.handler.logging.LoggingHandler:39) - [id: 0x00f6fd54, /10.43.18.160:52499 => /10.43.18.160:9104] CONNECTED: /10.43.18.160:52499
2012-11-28 17:54:12.342+0100 [SC_WORKER thread-13] DEBUG (org.jboss.netty.handler.logging.LoggingHandler:39) - [id: 0x00f6fd54, /10.43.18.160:52499 => /10.43.18.160:9104] CHANGE_INTEREST: 0
2012-11-28 17:54:12.420+0100 [New I/O server worker #9-20] DEBUG (org.jboss.netty.handler.logging.LoggingHandler:39) - [id: 0x00f6fd54, /10.43.18.160:52499 => /10.43.18.160:9104] RECEIVED: BigEndianHeapChannelBuffer(ridx=0, widx=501, cap=501) - (HEXDUMP: ....)
2012-11-28 17:54:12.435+0100 [SC_WORKER thread-13] DEBUG (org.jboss.netty.handler.logging.LoggingHandler:39) - [id: 0x00f6fd54, /10.43.18.160:52499 => /10.43.18.160:9104] INTEREST_CHANGED
2012-11-28 17:54:12.451+0100 [SC_WORKER thread-11] DEBUG (org.jboss.netty.handler.logging.LoggingHandler:43) - [id: 0x00f6fd54, /10.43.18.160:52499 => /10.43.18.160:9104] EXCEPTION: java.lang.NullPointerException
java.lang.NullPointerException
    at org.serviceconnector.net.res.netty.tcp.proxy.NettyTcpProxyResponderRequestHandler.messageReceived(NettyTcpProxyResponderRequestHandler.java:127)
    at org.jboss.netty.handler.execution.ChannelEventRunnable.run(ChannelEventRunnable.java:69)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:619) 

在调试模式下,我可以看到msg和outboundChannel都为null:/ 如果我将代码改为以下代码就可以了。

在出现错误时再次写入消息

try{
  outboundChannel.write(msg);
} catch(Throwable t) {
  outboundChannel.write(msg);
}

或减慢线程

Thread.sleep(200);
outboundChannel.write(msg);
猜猜我正面临着竞争状态。它只发生在慢速机器上 当然很难再现,不可能。我无法提供一个例子。

我尝试了netty 3.5.10.Final具有相同的行为。任何人观察到类似的行为? THX!

1 个答案:

答案 0 :(得分:3)

如果在outboundChannel之前调用null

messageReceived只能是channelOpen。通常,它永远不会发生,但如果您的管道在处理程序之前有ExecutionHandler并且您正在使用无序执行程序服务,则可能会发生这种情况。如果是,请使用ExecutorService,例如OrderedMemoryAwareThreadPoolExecutor