netty 處理遠程主機強制關閉一個鏈接

netty   處理遠程主機強制關閉一個鏈接,首先看下api解釋:java

/**
 * Returns {@code true} if and only if the channel should not close itself when its remote
 * peer shuts down output to make the connection half-closed.  If {@code false}, the connection
 * is closed automatically when the remote peer shuts down output.
 */
boolean isAllowHalfClosure();

/**
 * Sets whether the channel should not close itself when its remote peer shuts down output to
 * make the connection half-closed.  If {@code true} the connection is not closed when the
 * remote peer shuts down output. Instead,
 * {@link ChannelInboundHandler#userEventTriggered(ChannelHandlerContext, Object)}
 * is invoked with a {@link ChannelInputShutdownEvent} object. If {@code false}, the connection
 * is closed automatically.
 */
SocketChannelConfig setAllowHalfClosure(boolean allowHalfClosure);

默認是自動關閉channel,可是這樣沒法捕獲遠程主機強制關閉一個鏈接異常,因此 設置serverconfig react

allowHalfClosure=true

這裏初始化配置能夠在下面的方法中處理api

/**
 * This method will be called once the {@link Channel} was registered. After the method returns this instance
 * will be removed from the {@link ChannelPipeline} of the {@link Channel}.
 *
 * @param ch            the {@link Channel} which was registered.
 * @throws Exception    is thrown if an error occurs. In that case it will be handled by
 *                      {@link #exceptionCaught(ChannelHandlerContext, Throwable)} which will by default close
 *                      the {@link Channel}.
 */
protected abstract void initChannel(Channel ch) throws Exception;

像這樣:socket

ch.config().setAllowHalfClosure(true);

就能夠在ide

/**
 * Calls {@link ChannelHandlerContext#fireUserEventTriggered(Object)} to forward
 * to the next {@link ChannelInboundHandler} in the {@link ChannelPipeline}.
 *
 * Sub-classes may override this method to change behavior.
 */
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    ctx.fireUserEventTriggered(evt);
}

中捕獲遠程主機強制關閉一個鏈接了 想這樣處理,在本身的handler中重寫上面的方法:this

/**
 * 讀取數據超時   --->  判定鏈接斷開  ----> 釋放對應的socket鏈接
 * <p/>
 * 心跳處理
 * 鏈路讀寫超時處理
 *
 * @param ctx
 * @param evt
 * @throws Exception sub_reactor                          ChannelInputShutdownEvent.INSTANCE
 */
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    try {
        Channel channel = ctx.channel();
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent e = (IdleStateEvent) evt;
            if (e.state() == IdleState.READER_IDLE) {
                channel.close();  //call back channelInactive(ChannelHandlerContext ctx)
                if (logger.isDebugEnabled()) logger
                        .debug(channel.remoteAddress() + "---No data was received for a while ,read time out... ...");
            } //     because we are attaching  more importance to the read_idle time(timeout to rec)
            else if (e.state() == IdleState.WRITER_IDLE) { // No data was sent for a while.
                channel.close();
                if (logger.isDebugEnabled()) logger
                        .debug(channel.remoteAddress() + "---No data was sent for a while.write time out... ...");
            }
        } else if (evt instanceof ChannelInputShutdownEvent) {
            channel.close();//遠程主機強制關閉鏈接
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}


經過以上設置就能夠捕獲 並處理邏輯相關資源了 netty   處理遠程主機強制關閉一個鏈接debug

相關文章
相關標籤/搜索