心跳频率

This commit is contained in:
2025-12-17 15:17:55 +08:00
parent e2092532f5
commit 0f5aab78bc

View File

@@ -19,7 +19,7 @@ import java.util.Set;
public class HeartbeatMonitorTask {
private static final String HEARTBEAT_PREFIX = "user:heartbeat:";
private static final long HEARTBEAT_TIMEOUT = 7 * 60 * 1000L; // 前端每5分钟发送一次心跳设置超时时间7分钟
private static final long HEARTBEAT_TIMEOUT = 7 * 60L; // 前端每5分钟发送一次心跳设置超时时间7分钟
private final StringRedisTemplate stringRedisTemplate;
private final SpdmServiceFeignClient spdmServiceFeignClient;
@@ -40,13 +40,14 @@ public class HeartbeatMonitorTask {
LocalDateTime now = LocalDateTime.now();
for (String key : keys) {
log.info("[HeartbeatMonitorTask] check heartbeat timeout for key: {}", key);
Object value = stringRedisTemplate.opsForValue().get(key);
log.info("[HeartbeatMonitorTask] check heartbeat timeout for key:{}, value:{}", key, value);
if (value != null) {
try {
LocalDateTime lastHeartbeat = LocalDateTime.parse(value.toString());
LocalDateTime timeoutTime = lastHeartbeat.plusSeconds(HEARTBEAT_TIMEOUT);
log.info("[HeartbeatMonitorTask] now:{}, timeoutTime:{}", now, timeoutTime);
// 如果超过心跳超时时间,认为用户已离线
if (now.isAfter(timeoutTime)) {
// 截取前缀后的部分,再按冒号拆分
@@ -54,6 +55,7 @@ public class HeartbeatMonitorTask {
String[] parts = suffix.split(":", 2);
Long tenantId = Long.valueOf(parts[0]);
Long userId = Long.valueOf(parts[1]);
log.info("[HeartbeatMonitorTask] tenantId:{}, userId:{}", tenantId, userId);
handleUserOffline(userId, tenantId, lastHeartbeat);
// 清除过期的心跳记录
stringRedisTemplate.delete(key);