diff --git a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/manager/RocketEnergyAggregator.java b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/manager/RocketEnergyAggregator.java index c32f3e2b..f9af6bbd 100644 --- a/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/manager/RocketEnergyAggregator.java +++ b/rc-service/rc-service-other/other-application/src/main/java/com/red/circle/other/app/manager/RocketEnergyAggregator.java @@ -100,24 +100,32 @@ public class RocketEnergyAggregator { try { String lockKey = RocketKeys.LOCK.getKey(roomId); - + distributedLockUtil.executeWithLock(lockKey, 10L, () -> { - - // 原子性地取出所有数据 - List batch = new ArrayList<>(); - RocketEnergyAddCmd cmd; - while ((cmd = queue.poll()) != null) { - batch.add(cmd); - } - + + // 先 peek 不删除,等处理成功后再删除 + List batch = new ArrayList<>(queue); // 复制数据,不删除原队列 + if (batch.isEmpty()) { return null; } - // 处理这个房间的能量增加 - processRoomEnergyBatch(roomId, batch); + try { + // 处理这个房间的能量增加 + processRoomEnergyBatch(roomId, batch); + + // 处理成功后才删除队列中的数据 + for (int i = 0; i < batch.size(); i++) { + queue.poll(); + } + + } catch (Exception e) { + log.error("处理房间{}火箭能量失败,数据保留在队列中等待重试", roomId, e); + throw e; // 重新抛出,让外层处理 + } + return null; - + }, "火箭能量刷新操作中"); } catch (Exception e) {