去掉取消订阅时删除功能锁的逻辑
parent
2b6895b518
commit
d8aaa53ac2
|
|
@ -23,9 +23,7 @@ import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.SmartLifecycle;
|
import org.springframework.context.SmartLifecycle;
|
||||||
import org.springframework.dao.DataAccessException;
|
import org.springframework.dao.DataAccessException;
|
||||||
import org.springframework.data.redis.connection.RedisConnection;
|
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.RedisCallback;
|
||||||
import org.springframework.data.redis.core.ScanOptions;
|
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
@ -740,48 +738,10 @@ public class MqttMessageHandler implements SmartLifecycle {
|
||||||
log.error("【全量取消】Redis批量删除失败", e);
|
log.error("【全量取消】Redis批量删除失败", e);
|
||||||
throw new RuntimeException("全量取消订阅失败:" + e.getMessage());
|
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());
|
log.info("【全量取消】前端{}成功取消{}个设备的订阅", clientId, deviceSet.size());
|
||||||
return frontendTopics;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 实际业务中:查询指定用户名下的所有设备ID(需替换为你的DAO/Service逻辑)
|
* 实际业务中:查询指定用户名下的所有设备ID(需替换为你的DAO/Service逻辑)
|
||||||
* @return 设备ID列表
|
* @return 设备ID列表
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue