update异常后删除连接

This commit is contained in:
2023-11-06 13:20:07 +08:00
parent 65dbee64a1
commit df8f916c88
2 changed files with 20 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
package com.jsowell.common.enums.ykc;
import com.jsowell.common.util.StringUtils;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
@@ -61,4 +62,11 @@ public class PileChannelEntity {
manager.remove(pileSn);
}
public static void removeByChannelId(String channelId){
String pileSn = getPileSnByChannelId(channelId);
if (StringUtils.isNotBlank(pileSn)) {
manager.remove(pileSn);
}
}
}

View File

@@ -180,6 +180,8 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
if (flag) {
log.error("发生异常关闭链接channelId:{}, pileSn:{}", channelId.asShortText(), pileSn);
ctx.channel().close();
close(channelId);
}
}
}
@@ -201,6 +203,16 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
}
log.error("【{}】发生了错误, pileSn:【{}】此连接被关闭, 此时连通数量: {}", channelId, pileSn, CHANNEL_MAP.size());
ctx.channel().close();
close(channelId);
}
}
private void close(ChannelId channelId) {
// 删除桩编号和channel的关系
PileChannelEntity.removeByChannelId(channelId.asLongText());
// 删除连接
CHANNEL_MAP.remove(channelId);
}
}