fix: 已知问题修复
This commit is contained in:
@@ -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 {
|
||||
|
||||
|
||||
@@ -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 连接,避免阻塞应用停止
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user