Merge branch 'dev-new' into dev-new-rabbitmq

This commit is contained in:
Guoqs
2024-11-28 13:37:53 +08:00
8 changed files with 63 additions and 44 deletions

View File

@@ -5,7 +5,9 @@ import com.jsowell.common.constant.CacheConstants;
import com.jsowell.common.core.domain.ykc.YKCDataProtocol;
import com.jsowell.common.core.domain.ykc.YKCFrameTypeCode;
import com.jsowell.common.core.redis.RedisCache;
import com.jsowell.common.protocol.SyncPromise;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.RpcUtil;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.netty.factory.YKCOperateFactory;
import com.jsowell.pile.domain.PileReservationInfo;
@@ -36,7 +38,7 @@ public class ReservationChargingResponseHandler extends AbstractYkcHandler {
}
@Override
public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, ChannelHandlerContext channel) {
public byte[] supplyProcess(YKCDataProtocol ykcDataProtocol, ChannelHandlerContext ctx) {
// log.info("[====远程更新应答====] param:{}, channel:{}", JSON.toJSONString(ykcDataProtocol), channel.toString());
// 消息体
byte[] msgBody = ykcDataProtocol.getMsgBody();
@@ -55,7 +57,7 @@ public class ReservationChargingResponseHandler extends AbstractYkcHandler {
String pileSn = BytesUtil.bcd2Str(pileSnByteArr);
// 保存时间
saveLastTimeAndCheckChannel(pileSn, channel);
saveLastTimeAndCheckChannel(pileSn, ctx);
// 枪口号
startIndex += length;
@@ -78,6 +80,18 @@ public class ReservationChargingResponseHandler extends AbstractYkcHandler {
log.info("0x59预约充电响应, 交易流水号:{}, 桩SN:{}, 枪口号:{}, 结果:{}, 失败原因:{}",
transactionCode, pileSn, connectorCode, resultCode, failedReason);
// 根据请求id在集合中找到与外部线程通信的SyncPromise对象
String msgId = ctx.channel().id().toString() + "_" + YKCFrameTypeCode.RESERVATION_CHARGING_SETUP_CODE.getCode();
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();
}
// 如果收到成功, 从redis取值, 保存到数据库
if ("01".equals(resultCode)) {
// 预约成功, 删除redis中的预约信息

View File

@@ -50,7 +50,7 @@ public class TimeCheckSettingResponseHandler extends AbstractYkcHandler {
length = 7;
byte[] currentTimeByteArr = BytesUtil.copyBytes(msgBody, startIndex, length);
Date date = Cp56Time2aUtil.byte2Hdate(currentTimeByteArr);
log.info("对时设置应答, pileSn:{}, channelId:{}, 充电桩当前时间:{}", pileSn, channel.channel().id().asShortText(), DateUtils.formatDateTime(date));
log.info("[===对时设置充电桩应答===], pileSn:{}, channelId:{}, 充电桩当前时间:{}", pileSn, channel.channel().id().asShortText(), DateUtils.formatDateTime(date));
return null;
}
}

View File

@@ -51,9 +51,14 @@ public class NettyServerManager implements CommandLineRunner {
.handler(new LoggingHandler(LogLevel.DEBUG))
// .option(ChannelOption.SO_BACKLOG, 128) // 默认128
.option(ChannelOption.SO_BACKLOG, 1024)
.option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
// .option(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.SO_KEEPALIVE, true) // 保持连接
.childOption(ChannelOption.TCP_NODELAY, true) // 禁用 Nagle 算法
.childOption(ChannelOption.SO_RCVBUF, 64 * 1024) // 接收缓冲区
.childOption(ChannelOption.SO_SNDBUF, 64 * 1024) // 发送缓冲区
.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(32 * 1024, 64 * 1024)) // 写缓冲水位
.childOption(ChannelOption.SO_REUSEADDR, true)
.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) // 启用池化内存分配器
.childHandler(nettyServerChannelInitializer)
.localAddress(new InetSocketAddress(host, port));

View File

@@ -26,8 +26,8 @@ public class NettyServerChannelInitializer extends ChannelInitializer<SocketChan
// pipeline.addLast("frameDecoder", new YkcProtocolDecoder());
pipeline.addLast("decoder", new ByteArrayDecoder());
pipeline.addLast("encoder", new ByteArrayDecoder());
//读超时时间设置为10s0表示不监控
pipeline.addLast(new IdleStateHandler(60, 0, 0, TimeUnit.SECONDS));
// 读超时时间设置为30s0表示不监控
pipeline.addLast(new IdleStateHandler(30, 0, 0, TimeUnit.SECONDS));
pipeline.addLast("handler", nettyServerHandler);
}

View File

@@ -7,8 +7,6 @@ import com.jsowell.common.enums.ykc.PileChannelEntity;
import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.StringUtils;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.common.util.RpcUtil;
import com.jsowell.common.protocol.SyncPromise;
import com.jsowell.netty.service.yunkuaichong.YKCBusinessService;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
@@ -41,7 +39,7 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
*/
private static final ConcurrentHashMap<ChannelId, ChannelHandlerContext> CHANNEL_MAP = new ConcurrentHashMap<>();
private final List<String> notPrintFrameTypeList = Lists.newArrayList("0x03");
private final List<String> notPrintFrameTypeList = Lists.newArrayList(); // "0x03"
/**
* 有客户端连接服务器会触发此函数
@@ -136,21 +134,21 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
String frameType = YKCUtils.frameType2Str(frameTypeBytes);
// 判断该帧类型是否为某请求帧的应答帧
String requestFrameType = YKCFrameTypeCode.PileAnswersRelation.getRequestFrameType(frameType);
// 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();
}
}
// 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());