暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

关于Netty socket 通信 channel 资源释放关闭问题

纯洁的明依 2021-01-15
1583

关于Netty socket 通信 channel 资源释放关闭问题

 

当客户端或者服务端接收数据处理出现异常或者客户端主动关闭连接:会进入如下方法

 


此时 我们应该进行释放资源 channel 关闭。

eventLoop中注销channel

  @Override    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {        log.error("Error occurred on channel {}", ctx.channel(), cause);        ctx.close();    }}

 

还有一种情况:当使用ChannelPool .使用完要及时释放

 

private void release(ChannelHandlerContext ctx) {    Channel channel = ctx.channel();    ChannelFuture cf = channel.close();    if (null != pool) {        cf.addListener(future -> pool.release(channel));    }}
@Overrideprotected void channelRead0(ChannelHandlerContext ctx, MtMessage msg) { try { channelRead1(msg); } finally { release(ctx); }}


 

 


文章转载自纯洁的明依,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论