java.nio.channels.ClosedChannelException

qinbo 发布于 2013/01/28 16:41
阅读 20K+
收藏 0

服务器处理完业务并把处理结果返回给客户端,在关闭ChannelFuture对象时总汇报异常。

代码如下:

        DLPResponse response = protocol.getResponse();
        if (request.getReturnValue()==1){
             ChannelFuture future = channel.write(response);
             future.addListener(ChannelFutureListener.CLOSE);
        }这样总爆出异常。

异常如下:

at org.jboss.netty.channel.socket.nio.AbstractNioWorker.cleanUpWriteBuffer(AbstractNioWorker.java:778)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.writeFromUserCode(AbstractNioWorker.java:501)
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.handleAcceptedSocket(NioServerSocketPipelineSink.java:129)
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.eventSunk(NioServerSocketPipelineSink.java:66)
at org.jboss.netty.channel.Channels.write(Channels.java:733)
at org.jboss.netty.channel.Channels.write(Channels.java:694)
.....

请各位大侠指导!小弟真的不知道错在哪里?

加载中
0
Tsingxu
Tsingxu
你这个堆栈好像不全,是什么版本的netty ?
q
qinbo
很感谢您,经理刚才帮我解决了一下,自己的搭建的框架出现问题,导致不能正确读取ip信息,出现上面的问题!
Tsingxu
Tsingxu
回复 @qinbo : 下面的堆栈能不能贴一下。
q
qinbo
3.5.7版本的
0
q
qinbo
经理还没有解决,仍然爆出这样的问题:问题如下:
java.nio.channels.ClosedChannelException
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.cleanUpWriteBuffer(AbstractNioWorker.java:778)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.writeFromUserCode(AbstractNioWorker.java:501)
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.handleAcceptedSocket(NioServerSocketPipelineSink.java:129)
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.eventSunk(NioServerSocketPipelineSink.java:66)
at org.jboss.netty.channel.Channels.write(Channels.java:733)
at org.jboss.netty.channel.Channels.write(Channels.java:694)
at com.secisland.dlp.server.netty.server.DLPServerEncoder.writeRequested(DLPServerEncoder.java:59)
at org.jboss.netty.handler.execution.ExecutionHandler.handleDownstream(ExecutionHandler.java:185)
at org.jboss.netty.channel.Channels.write(Channels.java:712)
at org.jboss.netty.channel.Channels.write(Channels.java:679)
at org.jboss.netty.channel.AbstractChannel.write(AbstractChannel.java:248)
at com.secisland.dlp.server.netty.server.DLPServerHandler.messageReceived(DLPServerHandler.java:39)
at org.jboss.netty.handler.execution.ChannelUpstreamEventRunnable.doRun(ChannelUpstreamEventRunnable.java:45)
at org.jboss.netty.handler.execution.ChannelEventRunnable.run(ChannelEventRunnable.java:69)
at org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor$ChildExecutor.run(OrderedMemoryAwareThreadPoolExecutor.java:315)
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)
而:(DLPServerEncoder.java:59)的代码是编码的代码为:Channels.write(ctx, e.getFuture(), totalBuffer); 这句是我写的代码。请大侠帮我!
Tsingxu
Tsingxu
看堆栈没有什么问题,异常是在哪行语句被捕获到的?
0
q
qinbo
  headBuffer.flip();
        ChannelBuffer totalBuffer=ChannelBuffers.dynamicBuffer();
        totalBuffer.writeBytes(headBuffer);
        totalBuffer.writeBytes(dataBuffer);

       Channels.write(ctx, e.getFuture(), totalBuffer);

上面的最后一句话捕获的异常!

0
q
qinbo

引用来自“qinbo”的答案

经理还没有解决,仍然爆出这样的问题:问题如下:
java.nio.channels.ClosedChannelException
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.cleanUpWriteBuffer(AbstractNioWorker.java:778)
at org.jboss.netty.channel.socket.nio.AbstractNioWorker.writeFromUserCode(AbstractNioWorker.java:501)
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.handleAcceptedSocket(NioServerSocketPipelineSink.java:129)
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.eventSunk(NioServerSocketPipelineSink.java:66)
at org.jboss.netty.channel.Channels.write(Channels.java:733)
at org.jboss.netty.channel.Channels.write(Channels.java:694)
at com.secisland.dlp.server.netty.server.DLPServerEncoder.writeRequested(DLPServerEncoder.java:59)
at org.jboss.netty.handler.execution.ExecutionHandler.handleDownstream(ExecutionHandler.java:185)
at org.jboss.netty.channel.Channels.write(Channels.java:712)
at org.jboss.netty.channel.Channels.write(Channels.java:679)
at org.jboss.netty.channel.AbstractChannel.write(AbstractChannel.java:248)
at com.secisland.dlp.server.netty.server.DLPServerHandler.messageReceived(DLPServerHandler.java:39)
at org.jboss.netty.handler.execution.ChannelUpstreamEventRunnable.doRun(ChannelUpstreamEventRunnable.java:45)
at org.jboss.netty.handler.execution.ChannelEventRunnable.run(ChannelEventRunnable.java:69)
at org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor$ChildExecutor.run(OrderedMemoryAwareThreadPoolExecutor.java:315)
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)
而:(DLPServerEncoder.java:59)的代码是编码的代码为:Channels.write(ctx, e.getFuture(), totalBuffer); 这句是我写的代码。请大侠帮我!
  headBuffer.flip(); 
        ChannelBuffer totalBuffer=ChannelBuffers.dynamicBuffer(); 
        totalBuffer.writeBytes(headBuffer); 
        totalBuffer.writeBytes(dataBuffer); 

       Channels.write(ctx, e.getFuture(), totalBuffer);

上面的最后一句话捕获的异常!

Tsingxu
Tsingxu
写缓冲是不建议使用的 ,是Deprecated 方法
Tsingxu
Tsingxu
还有,写操作完全没必要缓冲,在channel中有写队列,即ExecutionHandler 的handleDown置为false
Tsingxu
Tsingxu
如果没有主动调用 channelFuture.setSuccess或者setFailure,多半是对端断掉的。
Tsingxu
Tsingxu
你试着从pipeline中选一个handler,重写channelDisconnected 方法,看看是谁触发的断连操作。
0
Gaischen
Gaischen
           ChannelFuture future = channel.write(response);

             future.addListener(ChannelFutureListener.CLOSE);

应该要循环向外写吧  你这样都没有写完 就把channel关闭了 肯定要报.ClosedChannelException

netty 没用过 估计一下 应该是这个问题把。。。

0
q
qinbo

问题解决了,问题不是出在服务端,客户端出了问题。客户端在没有接收到服务端发送的数据之前就已经关闭了连接,导致了上面的问题。

依然很感谢tsingxu及FrankHui等热心人帮我解决问题,菜鸟我很感激!

f
fengjidly
回复 @hellowword : 你好,你也遇到这个问题了吗?有没有解决办法呢? java.nio.channels.ClosedChannelException: null
f
fengjidly
虽然时间过去很久,想问下楼主怎么解决这个问题的呢
hellowword
hellowword
怎么解决的 如何处理所说的 数据发送之前就关闭了链接
返回顶部
顶部