java nio 强制关闭_netty 处理远程主机强制关闭一个连接

当远程主机强制关闭连接时,Netty默认会自动关闭Channel。为捕获此异常,可以设置`setAllowHalfClosure(true)`,然后在`userEventTriggered`方法中处理`ChannelInputShutdownEvent`,以便在接收到该事件时执行相应的关闭和资源释放操作。
摘要由CSDN通过智能技术生成

netty   处理远程主机强制关闭一个连接,首先看下api解释:

/**

* 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

allowHalfClosure=true

这里初始化配置可以在下面的方法中处理

/**

* 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;

像这样:

ch.config().setAllowHalfClosure(true);

就可以在

/**

* 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中重写上面的方法:

/**

* 读取数据超时   --->  断定连接断开  ----> 释放对应的socket连接

* 心跳处理

* 链路读写超时处理

*

* @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   处理远程主机强制关闭一个连接

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值