An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means

问题描述:

警告: An exceptionCaught() event was fired,and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
java.io.IOException: 远程主机强迫关闭一个现有的连接。
    at sun.nio.ch.socketdispatcher.read0(Native Method)
    at sun.nio.ch.socketdispatcher.read(UnkNown Source)
    at sun.nio.ch.IoUtil.readIntoNativeBuffer(UnkNown Source)
    at sun.nio.ch.IoUtil.read(UnkNown Source)
    at sun.nio.ch.socketChannelImpl.read(UnkNown Source)
    at io.netty.buffer.UnpooledUnsafeDirectByteBuf.setBytes(UnpooledUnsafeDirectByteBuf.java:446)
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:871)
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:208)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:119)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:485)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:452)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:346)
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:794)
    at java.lang.Thread.run(UnkNown Source)

我这里问题发生原因是服务端未给客户端回应消息前客户端提前关闭连接造成。

解决方案:

@Override
    public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause) throws Exception {
        super.exceptionCaught(ctx,cause);
        Channel channel = ctx.channel();
        //……
        if(channel.isActive())ctx.close();
    }

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...