【问题标题】:NPE in channel.write(Object message)channel.write(对象消息)中的 NPE
【发布时间】:2012-12-03 13:39:48
【问题描述】:

我正在运行一个基于 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 都为空:/ 如果我将代码更改为以下代码,它可以工作。

如果出现错误,只需重新编写消息

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

或者放慢线程

Thread.sleep(200);
outboundChannel.write(msg);

我想我正面临着比赛条件。它只发生在慢速机器上 当然它很难复制,unfor。我无法提供示例。

我用相同的行为尝试了 netty 3.5.10.Final。有人观察到类似的行为吗? 谢谢!

【问题讨论】:

标签: netty


【解决方案1】:

如果messageReceivedchannelOpen 之前被调用,outboundChannel 只能是null。通常,它永远不会发生,但如果您的管道在处理程序之前有一个 ExecutionHandler 并且您使用的是无序执行器服务,则它可能会发生。如果是这样,请使用ExecutorService,例如OrderedMemoryAwareThreadPoolExecutor

【讨论】:

  • 非常抱歉,显然我没有仔细阅读 ExecutionHandler API:/!
猜你喜欢
  • 2014-11-03
  • 2013-11-26
  • 2016-08-08
  • 1970-01-01
  • 2020-07-27
  • 1970-01-01
  • 2017-12-02
  • 2022-01-26
  • 2021-07-12
相关资源
最近更新 更多