手动释放对象

This commit is contained in:
Guoqs
2024-12-28 13:44:57 +08:00
parent 458c216a6d
commit c9bfa8ace8
4 changed files with 173 additions and 152 deletions

View File

@@ -231,25 +231,31 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
String socketString = ctx.channel().remoteAddress().toString();
ChannelId channelId = ctx.channel().id();
String pileSn = PileChannelEntity.getPileSnByChannelId(channelId.asLongText());
if (evt instanceof IdleStateEvent) { // 超时事件
IdleStateEvent event = (IdleStateEvent) evt;
boolean flag = false;
if (event.state() == IdleState.READER_IDLE) { // 读
flag = true;
// log.error("Client-IP:【{}】, channelId:【{}】, pileSn:【{}】, READER_IDLE 读超时", socketString, channelId, pileSn);
} else if (event.state() == IdleState.WRITER_IDLE) { // 写
flag = true;
// log.error("Client-IP:【{}】, channelId:【{}】, pileSn:【{}】, WRITER_IDLE 写超时", socketString, channelId, pileSn);
} else if (event.state() == IdleState.ALL_IDLE) { // 全部
flag = true;
// log.error("Client-IP:【{}】, channelId:【{}】, pileSn:【{}】, ALL_IDLE 总超时", socketString, channelId, pileSn);
try {
String socketString = ctx.channel().remoteAddress().toString();
ChannelId channelId = ctx.channel().id();
String pileSn = PileChannelEntity.getPileSnByChannelId(channelId.asLongText());
if (evt instanceof IdleStateEvent) { // 超时事件
IdleStateEvent event = (IdleStateEvent) evt;
boolean flag = false;
if (event.state() == IdleState.READER_IDLE) { // 读
flag = true;
// log.error("Client-IP:【{}】, channelId:【{}】, pileSn:【{}】, READER_IDLE 读超时", socketString, channelId, pileSn);
} else if (event.state() == IdleState.WRITER_IDLE) { // 写
flag = true;
// log.error("Client-IP:【{}】, channelId:【{}】, pileSn:【{}】, WRITER_IDLE 写超时", socketString, channelId, pileSn);
} else if (event.state() == IdleState.ALL_IDLE) { // 全部
flag = true;
// log.error("Client-IP:【{}】, channelId:【{}】, pileSn:【{}】, ALL_IDLE 总超时", socketString, channelId, pileSn);
}
if (flag) {
ctx.channel().close();
// close(channelId, pileSn);
}
}
if (flag) {
ctx.channel().close();
// close(channelId, pileSn);
} finally {
if (evt instanceof ByteBuf) {
ReferenceCountUtil.release(evt);
}
}
}
@@ -259,20 +265,26 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ChannelId channelId = ctx.channel().id();
String channelIdShortText = channelId.asShortText();
String pileSn = PileChannelEntity.getPileSnByChannelId(channelIdShortText);
log.error("发生异常 channelId:{}, pileSn:{}", channelIdShortText, pileSn, cause);
cause.printStackTrace();
// 如果桩连到平台在1分钟内没有发送数据过来会报ReadTimeoutException异常
if (cause instanceof ReadTimeoutException) {
if (log.isTraceEnabled()) {
log.trace("Connection timeout 【{}】", ctx.channel().remoteAddress());
try {
ChannelId channelId = ctx.channel().id();
String channelIdShortText = channelId.asShortText();
String pileSn = PileChannelEntity.getPileSnByChannelId(channelIdShortText);
log.error("发生异常 channelId:{}, pileSn:{}", channelIdShortText, pileSn, cause);
cause.printStackTrace();
// 如果桩连到平台在1分钟内没有发送数据过来会报ReadTimeoutException异常
if (cause instanceof ReadTimeoutException) {
if (log.isTraceEnabled()) {
log.trace("Connection timeout 【{}】", ctx.channel().remoteAddress());
}
log.error("【{}】发生了错误, pileSn:【{}】此连接被关闭, 此时连通数量: {}", channelId, pileSn, CHANNEL_MAP.size());
ctx.channel().close();
}
} finally {
if (ctx.channel().isActive()) {
ctx.close();
}
log.error("【{}】发生了错误, pileSn:【{}】此连接被关闭, 此时连通数量: {}", channelId, pileSn, CHANNEL_MAP.size());
ctx.channel().close();
}
// close(channelId, pileSn);
}