手动释放对象

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,57 +128,39 @@ 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 {
YKCDataProtocol ykcDataProtocol = (YKCDataProtocol) message; try {
YKCDataProtocol ykcDataProtocol = (YKCDataProtocol) message;
// 获取帧类型 // 获取帧类型
byte[] frameTypeBytes = ykcDataProtocol.getFrameType(); byte[] frameTypeBytes = ykcDataProtocol.getFrameType();
String frameType = YKCUtils.frameType2Str(frameTypeBytes); String frameType = YKCUtils.frameType2Str(frameTypeBytes);
// 获取序列号域
// 判断该帧类型是否为某请求帧的应答帧 int serialNumber = BytesUtil.bytesToIntLittle(ykcDataProtocol.getSerialNumber());
// String requestFrameType = YKCFrameTypeCode.PileAnswersRelation.getRequestFrameType(frameType); // 获取channel
// log.info("同步获取响应数据-判断该帧类型是否为某请求帧的应答帧, frameType:{}, requestFrameType:{}", frameType, requestFrameType); Channel channel = ctx.channel();
// if (StringUtils.isNotBlank(requestFrameType)) { // 心跳包0x03日志太多造成日志文件过大改为不打印
// // 根据请求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());
// 获取channel
Channel channel = ctx.channel();
// 心跳包0x03日志太多造成日志文件过大改为不打印
if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
log.info("【<<<<<平台收到消息<<<<<】channel:{}, 帧类型:{}, 帧名称:{}, 序列号域:{}, 报文:{}",
channel.id(), frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber,
BytesUtil.binary(ykcDataProtocol.getBytes(), 16));
}
// 处理数据
byte[] response = ykcService.process(ykcDataProtocol, ctx);
if (Objects.nonNull(response)) {
// 响应客户端
ByteBuf buffer = ctx.alloc().buffer().writeBytes(response);
this.channelWrite(channel.id(), buffer);
if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) { if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
// 应答帧类型 log.info("【<<<<<平台收到消息<<<<<】channel:{}, 帧类型:{}, 帧名称:{}, 序列号域:{}, 报文:{}",
byte[] responseFrameTypeBytes = YKCFrameTypeCode.PlatformAnswersRelation.getResponseFrameTypeBytes(frameTypeBytes); channel.id(), frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber,
String responseFrameType = YKCUtils.frameType2Str(responseFrameTypeBytes); BytesUtil.binary(ykcDataProtocol.getBytes(), 16));
log.info("【>>>>>平台响应消息>>>>>】channel:{}, 响应帧类型:{}, 响应帧名称:{}, 原帧类型:{}, 原帧名称:{}, 序列号域:{}, response:{}",
channel.id(), responseFrameType, YKCFrameTypeCode.getFrameTypeStr(responseFrameType),
frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber,
BytesUtil.binary(response, 16));
} }
// 处理数据
byte[] response = ykcService.process(ykcDataProtocol, ctx);
if (Objects.nonNull(response)) {
// 响应客户端
ByteBuf buffer = ctx.alloc().buffer().writeBytes(response);
this.channelWrite(channel.id(), buffer);
if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
// 应答帧类型
byte[] responseFrameTypeBytes = YKCFrameTypeCode.PlatformAnswersRelation.getResponseFrameTypeBytes(frameTypeBytes);
String responseFrameType = YKCUtils.frameType2Str(responseFrameTypeBytes);
log.info("【>>>>>平台响应消息>>>>>】channel:{}, 响应帧类型:{}, 响应帧名称:{}, 原帧类型:{}, 原帧名称:{}, 序列号域:{}, response:{}",
channel.id(), responseFrameType, YKCFrameTypeCode.getFrameTypeStr(responseFrameType),
frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber,
BytesUtil.binary(response, 16));
}
}
} finally {
ReferenceCountUtil.release(message);
} }
} }