去掉取消订阅时删除功能锁的逻辑

feasure
xce 2026-01-18 16:25:16 +08:00
parent 2b6895b518
commit d8aaa53ac2
1 changed files with 0 additions and 40 deletions

View File

@ -23,9 +23,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.SmartLifecycle;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;
@ -740,48 +738,10 @@ public class MqttMessageHandler implements SmartLifecycle {
log.error("【全量取消】Redis批量删除失败", e);
throw new RuntimeException("全量取消订阅失败:" + e.getMessage());
}
// 6. 清理该前端持有的分布式锁(精准扫描,提升效率)
Set<String> lockKeys = scanRedisKeys("lock:*:" + clientId);
if (lockKeys != null && !lockKeys.isEmpty()) {
stringRedisTemplate.delete(lockKeys); // 批量删除锁,无需遍历
log.info("【全量取消】清理前端{}持有的{}个分布式锁", clientId, lockKeys.size());
}
log.info("【全量取消】前端{}成功取消{}个设备的订阅", clientId, deviceSet.size());
return frontendTopics;
}
// 保留原有scanRedisKeys方法兼容JDK 8
private Set<String> scanRedisKeys(String pattern) {
Set<String> keys = new HashSet<>();
try {
stringRedisTemplate.execute(new RedisCallback<Void>() {
@Override
public Void doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = stringRedisTemplate.getStringSerializer();
ScanOptions scanOptions = ScanOptions.scanOptions()
.match(pattern)
.count(100)
.build();
Cursor<byte[]> cursor = connection.scan(scanOptions);
while (cursor.hasNext()) {
byte[] keyBytes = cursor.next();
String key = serializer.deserialize(keyBytes);
if (key != null) {
keys.add(key);
}
}
cursor.close();
return null;
}
});
} catch (Exception e) {
log.error("Redis Scan查询失败pattern={}", pattern, e);
}
return keys;
}
/**
* IDDAO/Service
* @return ID