修改红中资源占用问题

master
zhouwei 2026-02-28 14:13:44 +08:00
parent 0c1cee78c5
commit f855be8d92
3 changed files with 40 additions and 15 deletions

View File

@ -23,6 +23,9 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import static robot.mj.thread.ThreadPoolConfig.scheduleDelay;
/**
* -
@ -157,7 +160,10 @@ public class EXGameController extends GameController {
GroupRoomBusiness.joinRoom(groupId, roomId, robotSession, null);
Thread.sleep(5000);
//使用非阻塞延迟替代Thread.sleep
scheduleDelay(() -> {
}, 5, TimeUnit.SECONDS);
params.del("groupId");
params.del("roomId");
@ -353,7 +359,10 @@ public class EXGameController extends GameController {
//先不放入映射 等确认加入成功后再放入
//robotRoomMapping.put(robotUser.getConnecId(), robotUser);
robotRoomMapping.remove(robotUser.getRobotId());
Thread.sleep(2000);
//非阻塞延迟替代Thread.sleep
scheduleDelay(() -> {
}, 2, TimeUnit.SECONDS);
params.putString("session", "{user}:" + robotId + "," + robotSession);
//发送加入房间请求到game_mj_cs
@ -376,15 +385,21 @@ public class EXGameController extends GameController {
//添加超时检查机制
CompletableFuture.runAsync(() -> {
try {
Thread.sleep(15000);
//定时任务替代Thread.sleep
scheduleDelay(() -> {
//15秒后还没有建立映射关系 加入可能失败
if (robotRoomMapping.get(robotUser.getConnecId()) == null) {
System.err.println("机器人{"+robotId+"}加入房间{"+roomId+"}超时,清理临时状态");
//清理可能残留的状态
robotConnectionManager.disconnectFromGameServer(connecId);
}
} catch (InterruptedException e) {
log.error("加入房间超时异常");
}, 15, TimeUnit.SECONDS);
//15秒后还没有建立映射关系 加入可能失败
if (robotRoomMapping.get(robotUser.getConnecId()) == null) {
System.err.println("机器人{"+robotId+"}加入房间{"+roomId+"}超时,清理临时状态");
robotConnectionManager.disconnectFromGameServer(connecId);
}
} catch (Exception e) {
log.error("机器人加入房间超时", e);
}
}, ThreadPoolConfig.getBusinessThreadPool());//指定自定义线程池
robotUser.setIntoRoomTime(robotConnectionManager.getTime());

View File

@ -47,7 +47,6 @@ public class RobotConnectionManager {
private final String host= Config.GAME_SERVER_HOST;
private final int port= Integer.parseInt(Config.GAME_SERVER_PORT);
public RobotConnectionManager() {
exGameController = new EXGameController();
}
@ -128,12 +127,16 @@ public class RobotConnectionManager {
HuNanHongZhong instance = huNanHongZhongInstances.get(connecId);
if (instance != null) {
//清理所有集合数据以释放内存
instance.getHongZhongCardInhand().clear();
instance.getChuGuoCardInhand().clear();
System.out.println("清空HuNanHongZhong集合数据: " + connecId);
}
//移除实例
huNanHongZhongInstances.remove(connecId);
System.out.println("清理完成,当前活跃连接数: " + activeConnections.size() + ", 实例数: " + huNanHongZhongInstances.size());
}
if (robotUser != null) {
@ -356,17 +359,17 @@ public class RobotConnectionManager {
Jedis jedis2 = Redis.use("group1_db2").getJedis();
try {
//红中麻将 机器人处理事件
//出牌广播
if ("812".equalsIgnoreCase(command)) {
huNanHongZhong.drawCard(command, message);
//初始化手牌
if ("811".equalsIgnoreCase(command)) {
robotUser.setStatus(ROBOTEventType.ROBOT_INTOROOM_WORKING);
huNanHongZhong.cardInHead(command, message, client);
//处理完协议后保存到Redis
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
currentInstance.saveToRedis(connecId);
}
//初始化手牌
else if ("811".equalsIgnoreCase(command)) {
robotUser.setStatus(ROBOTEventType.ROBOT_INTOROOM_WORKING);
huNanHongZhong.cardInHead(command, message, client);
//出牌广播
else if ("812".equalsIgnoreCase(command)) {
huNanHongZhong.drawCard(command, message);
//处理完协议后保存到Redis
HuNanHongZhong currentInstance = huNanHongZhongInstances.get(connecId);
currentInstance.saveToRedis(connecId);

View File

@ -21,6 +21,8 @@ public class ResourceCleanupUtil {
*/
public static void performCleanup() {
if (pendingCleanupResources.isEmpty()) {
//执行常规清理
performRegularCleanup();
return;
}
@ -32,6 +34,8 @@ public class ResourceCleanupUtil {
for (String resourceId : resourcesToClean) {
try {
//这里可以根据resourceId的具体类型执行不同的清理逻辑
//暂时保持原有逻辑
//从待清理列表中移除
pendingCleanupResources.remove(resourceId);
@ -44,6 +48,9 @@ public class ResourceCleanupUtil {
}
System.out.println("资源清理完成,共清理: " + cleanedCount + " 个资源");
//执行常规清理
performRegularCleanup();
}
/**