火箭能量异步刷新数据丢失 问题处理

This commit is contained in:
tianfeng 2025-11-18 01:18:09 +08:00
parent 3cbcb484cc
commit b39b6ac26c

View File

@ -100,24 +100,32 @@ public class RocketEnergyAggregator {
try {
String lockKey = RocketKeys.LOCK.getKey(roomId);
distributedLockUtil.executeWithLock(lockKey, 10L, () -> {
// 原子性地取出所有数据
List<RocketEnergyAddCmd> batch = new ArrayList<>();
RocketEnergyAddCmd cmd;
while ((cmd = queue.poll()) != null) {
batch.add(cmd);
}
// peek 不删除等处理成功后再删除
List<RocketEnergyAddCmd> 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) {