在IOException上没有调用Netty ChannelFuture.operationComplete()

时间:2012-04-04 09:26:51

标签: netty

我在连接失败期间测试我的客户端服务器处理。

在客户端,当我从客户端向服务器发送消息时拔下LAN电缆时,我的ChannelFuture(下面)没有被调用。 我所期待的是异常将被我的ChannelFuture捕获,所以我可以在operationComplete中处理它。

相反,它只会被客户端管道中的最后一个上游处理程序捕获。 难道我做错了什么?或者这可能是Netty 3.2.4的问题?

当我拔下LAN电缆时,我在处理程序中遇到异常:

    java.io.IOException: An existing connection was forcibly closed by the remote host
        at sun.nio.ch.SocketDispatcher.read0(Native Method)
        at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:25)
        at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:198)
        at sun.nio.ch.IOUtil.read(IOUtil.java:166)
        at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:243)
        at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:321)
        at org.jboss.netty.channel.socket.nio.NioWorker.processSelectedKeys(NioWorker.java:280)
        at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:200)
        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:662)

My ChannelFuture:

    ChannelFuture future = Channels.write(channel, message);
    future.addListener(new ChannelFutureListener() {

           @Override
           public void operationComplete(ChannelFuture future) throws Exception {
               if (!future.isSuccess()) {
                   LOG.error("Client failed to send message", future.getCause());
                   future.getChannel().close();
               }
           }
    });

1 个答案:

答案 0 :(得分:0)

从堆栈看起来,写入已成功完成。在尝试从频道中读取时抛出了异常。如果您对这种异常感兴趣,则需要实现自己的SimpleChannelUpstreamHandler并覆盖exceptionCaught(..)方法。

务必将处理程序添加到ChannelPipeline。