手动释放对象

This commit is contained in:
Guoqs
2024-12-28 11:35:14 +08:00
parent 05a3e20cd2
commit 2ff36a6ed7

View File

@@ -13,6 +13,7 @@ import io.netty.channel.*;
import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent; import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.ReadTimeoutException; import io.netty.handler.timeout.ReadTimeoutException;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -127,42 +128,21 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object message) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object message) throws Exception {
try {
YKCDataProtocol ykcDataProtocol = (YKCDataProtocol) message; YKCDataProtocol ykcDataProtocol = (YKCDataProtocol) message;
// 获取帧类型 // 获取帧类型
byte[] frameTypeBytes = ykcDataProtocol.getFrameType(); byte[] frameTypeBytes = ykcDataProtocol.getFrameType();
String frameType = YKCUtils.frameType2Str(frameTypeBytes); String frameType = YKCUtils.frameType2Str(frameTypeBytes);
// 判断该帧类型是否为某请求帧的应答帧
// String requestFrameType = YKCFrameTypeCode.PileAnswersRelation.getRequestFrameType(frameType);
// log.info("同步获取响应数据-判断该帧类型是否为某请求帧的应答帧, frameType:{}, requestFrameType:{}", frameType, requestFrameType);
// if (StringUtils.isNotBlank(requestFrameType)) {
// // 根据请求id在集合中找到与外部线程通信的SyncPromise对象
// String msgId = ctx.channel().id().toString() + "_" + requestFrameType;
// // log.info("同步获取响应数据-收到消息, msgId:{}", msgId);
// SyncPromise syncPromise = RpcUtil.getSyncPromiseMap().get(msgId);
// if(syncPromise != null) {
// // 设置响应结果
// syncPromise.setRpcResult(ykcDataProtocol.getBytes());
// // 唤醒外部线程
// // log.info("同步获取响应数据-唤醒外部线程, SyncPromise:{}", JSON.toJSONString(syncPromise));
// syncPromise.wake();
// }
// }
// 获取序列号域 // 获取序列号域
int serialNumber = BytesUtil.bytesToIntLittle(ykcDataProtocol.getSerialNumber()); int serialNumber = BytesUtil.bytesToIntLittle(ykcDataProtocol.getSerialNumber());
// 获取channel // 获取channel
Channel channel = ctx.channel(); Channel channel = ctx.channel();
// 心跳包0x03日志太多造成日志文件过大改为不打印 // 心跳包0x03日志太多造成日志文件过大改为不打印
if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) { if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
log.info("【<<<<<平台收到消息<<<<<】channel:{}, 帧类型:{}, 帧名称:{}, 序列号域:{}, 报文:{}", log.info("【<<<<<平台收到消息<<<<<】channel:{}, 帧类型:{}, 帧名称:{}, 序列号域:{}, 报文:{}",
channel.id(), frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber, channel.id(), frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber,
BytesUtil.binary(ykcDataProtocol.getBytes(), 16)); BytesUtil.binary(ykcDataProtocol.getBytes(), 16));
} }
// 处理数据 // 处理数据
byte[] response = ykcService.process(ykcDataProtocol, ctx); byte[] response = ykcService.process(ykcDataProtocol, ctx);
if (Objects.nonNull(response)) { if (Objects.nonNull(response)) {
@@ -179,6 +159,9 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
BytesUtil.binary(response, 16)); BytesUtil.binary(response, 16));
} }
} }
} finally {
ReferenceCountUtil.release(message);
}
} }
// @Override // @Override