修改红中资源占用问题

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

View File

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

View File

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