使用ChannelHandlerContext

This commit is contained in:
Guoqs
2024-08-01 11:43:53 +08:00
parent 1cec3f64c5
commit 3ac1d5752c
6 changed files with 30 additions and 35 deletions

View File

@@ -2,6 +2,7 @@ package com.jsowell.common.enums.ykc;
import com.jsowell.common.util.StringUtils;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
@@ -16,35 +17,35 @@ public class PileChannelEntity {
/**
* 管理一个全局map保存连接进服务端的通道数量
*/
private static final ConcurrentHashMap<String, Channel> manager = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String, ChannelHandlerContext> manager = new ConcurrentHashMap<>();
/**
* 校验channel是否保存
*/
public static void checkChannel(String pileSn, Channel channel) {
public static void checkChannel(String pileSn, ChannelHandlerContext ctx) {
if (!manager.containsKey(pileSn)) {
// 如果manager中不存在pileSn的连接则保存
log.info("checkChannel-manager中不存在pileSn:{}的连接,保存新的channel:{}", pileSn, channel.id().asLongText());
manager.put(pileSn, channel);
log.info("checkChannel-manager中不存在pileSn:{}的连接,保存新的channel:{}", pileSn, ctx.channel().id().asLongText());
manager.put(pileSn, ctx);
return;
}
// 如果manager中存在pileSn的连接取出来对比
Channel sourceChannel = manager.get(pileSn);
Channel sourceChannel = manager.get(pileSn).channel();
if (sourceChannel == null) {
// 为空就put
log.info("checkChannel-manager中pileSn:{}的连接为空,保存新的channel:{}", pileSn, channel.id().asLongText());
manager.put(pileSn, channel);
log.info("checkChannel-manager中pileSn:{}的连接为空,保存新的channel:{}", pileSn, ctx.channel().id().asLongText());
manager.put(pileSn, ctx);
return;
}
// 两个做对比
String sourceChannelId = sourceChannel.id().asLongText();
String channelId = channel.id().asLongText();
String channelId = ctx.channel().id().asLongText();
if (!StringUtils.equals(sourceChannelId, channelId)) {
// 不一致则更新
log.info("checkChannel-manager中pileSn:{}的连接不一致, 老channelId:{}, 保存新的channel:{}", pileSn, sourceChannelId, channelId);
manager.put(pileSn, channel);
manager.put(pileSn, ctx);
}
}
@@ -53,7 +54,7 @@ public class PileChannelEntity {
* @param pileSn
* @return
*/
public static Channel getChannelByPileSn(String pileSn) {
public static ChannelHandlerContext getChannelByPileSn(String pileSn) {
return manager.get(pileSn);
}
@@ -63,8 +64,8 @@ public class PileChannelEntity {
* @return
*/
public static String getPileSnByChannelId(String channelId) {
for (HashMap.Entry<String, Channel> entry : manager.entrySet()) {
if (entry.getValue().id().asLongText().equals(channelId)) {
for (HashMap.Entry<String, ChannelHandlerContext> entry : manager.entrySet()) {
if (entry.getValue().channel().id().asLongText().equals(channelId)) {
return entry.getKey();
}
}
@@ -75,9 +76,9 @@ public class PileChannelEntity {
* 打印
*/
public static void output() {
for (HashMap.Entry<String, Channel> entry : manager.entrySet()) {
for (HashMap.Entry<String, ChannelHandlerContext> entry : manager.entrySet()) {
System.out.println("pileSn:" + entry.getKey() +
",ChannelId:" + entry.getValue().id().asLongText());
",ChannelId:" + entry.getValue().channel().id().asLongText());
}
}