fix: 已知问题修复

This commit is contained in:
Ray.Hao
2026-04-25 16:29:32 +08:00
parent b0422ea695
commit 7a43e9c38c
12 changed files with 83 additions and 605 deletions

View File

@@ -2,6 +2,7 @@ package com.youlai.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* 应用启动类
@@ -9,6 +10,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @author Ray.Hao
* @since 0.0.1
*/
@EnableScheduling
@SpringBootApplication
public class YouLaiBootApplication {

View File

@@ -6,10 +6,12 @@ import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -198,6 +200,28 @@ public class SseSessionRegistry {
}
}
/**
* 心跳检测每30秒向所有连接发送ping事件及时清理已断开的僵尸连接
*/
@Scheduled(fixedRate = 30000)
public void heartbeat() {
if (emitterUserMap.isEmpty()) {
return;
}
List<SseEmitter> failedEmitters = new ArrayList<>();
for (SseEmitter emitter : emitterUserMap.keySet()) {
try {
emitter.send(SseEmitter.event().name("ping").data("heartbeat"));
} catch (Exception e) {
failedEmitters.add(emitter);
}
}
if (!failedEmitters.isEmpty()) {
log.debug("心跳检测清理{}个失效SSE连接", failedEmitters.size());
failedEmitters.forEach(this::removeEmitter);
}
}
/**
* 容器关闭时主动断开所有 SSE 连接,避免阻塞应用停止
*/