|
@@ -13,6 +13,10 @@ import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
|
|
|
|
+import java.util.ArrayDeque;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Deque;
|
|
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
|
@@ -45,6 +49,22 @@ public class SseService {
|
|
|
*/
|
|
*/
|
|
|
private final Map<Long, SseEmitter> emitters = new ConcurrentHashMap<>();
|
|
private final Map<Long, SseEmitter> emitters = new ConcurrentHashMap<>();
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 重放缓冲上限:按条数限制内存(单条 JSON 一般 1~6KB,500 条约 3MB/用户)。
|
|
|
|
|
+ */
|
|
|
|
|
+ private static final int REPLAY_BUFFER_MAX = 500;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 用户没有活跃 SSE 连接期间的推送缓冲(按用户隔离,FIFO 有界)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>为什么需要</b>:智能清洗在受理请求返回的瞬间就开始推日志,而前端要等
|
|
|
|
|
+ * 「跳转 → 进度页挂载 → fetch 建立」才有连接,中间的推送全部落在无连接窗口里;
|
|
|
|
|
+ * 用户刷新页面(进度日志只存内存)或中途断网重连时也一样。没有缓冲时这些消息
|
|
|
|
|
+ * 直接丢弃,表现就是「日志只输出了一部分」。这里把无连接期间的每条推送暂存,
|
|
|
|
|
+ * 连接建立时按序重放(见 {@link #see()}),收口({@link #closeSee(Long)})时清空。
|
|
|
|
|
+ */
|
|
|
|
|
+ private final Map<Long, Deque<String>> pendingMessages = new ConcurrentHashMap<>();
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 建立 SSE 连接:创建 emitter、保存引用并注册生命周期回调,由控制器返回给前端
|
|
* 建立 SSE 连接:创建 emitter、保存引用并注册生命周期回调,由控制器返回给前端
|
|
|
*/
|
|
*/
|
|
@@ -68,6 +88,24 @@ public class SseService {
|
|
|
emitters.remove(userId, emitter);
|
|
emitters.remove(userId, emitter);
|
|
|
log.error("sse 连接异常: userId={}", userId, e);
|
|
log.error("sse 连接异常: userId={}", userId, e);
|
|
|
});
|
|
});
|
|
|
|
|
+ // 先重放断线/刷新期间积压的消息,再发本连接的心跳 —— 前端「任务日志」才能接上历史
|
|
|
|
|
+ Deque<String> pending = pendingMessages.remove(userId);
|
|
|
|
|
+ if (pending != null && !pending.isEmpty()) {
|
|
|
|
|
+ List<String> replay;
|
|
|
|
|
+ synchronized (pending) {
|
|
|
|
|
+ replay = new ArrayList<>(pending);
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("sse 重连重放: userId={}, 消息={} 条", userId, replay.size());
|
|
|
|
|
+ for (String msg : replay) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ emitter.send(SseEmitter.event().id(RandomUtil.randomString(32)).data(msg).reconnectTime(5000L));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ // 重放中途连接又断了:剩余消息放弃,后续推送会重新进缓冲
|
|
|
|
|
+ log.error("sse 重放失败,中止重放: userId={}", userId, e);
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
// 连接建立后立即推送一条心跳,前端 onopen 即刻触发
|
|
// 连接建立后立即推送一条心跳,前端 onopen 即刻触发
|
|
|
try {
|
|
try {
|
|
|
emitter.send(SseEmitter.event().id(RandomUtil.randomString(32))
|
|
emitter.send(SseEmitter.event().id(RandomUtil.randomString(32))
|
|
@@ -97,7 +135,9 @@ public class SseService {
|
|
|
public void sendSee(Long userId, SseDTO dto) {
|
|
public void sendSee(Long userId, SseDTO dto) {
|
|
|
SseEmitter emitter = userId == null ? null : emitters.get(userId);
|
|
SseEmitter emitter = userId == null ? null : emitters.get(userId);
|
|
|
if (emitter == null) {
|
|
if (emitter == null) {
|
|
|
- log.info("用户 {} 没有活跃的 sse 连接,跳过推送", userId);
|
|
|
|
|
|
|
+ // 无活跃连接不等于「可以丢」:进度/日志推送先进重放缓冲,等连接建立时补发。
|
|
|
|
|
+ // 消息本身带 caseId,前端按案件过滤,重放到旧案件的消息不会被误处理。
|
|
|
|
|
+ bufferPending(userId, dto);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
@@ -112,6 +152,27 @@ public class SseService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /** 无连接期间的推送入缓冲(FIFO 有界,超出丢最旧),留给下次连接重放 */
|
|
|
|
|
+ private void bufferPending(Long userId, SseDTO dto) {
|
|
|
|
|
+ if (userId == null) {
|
|
|
|
|
+ log.info("用户无 SSE 连接且无法确定用户身份,丢弃推送: cmd={}", dto == null ? null : dto.cmd());
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ String msg = Json.toStr(dto.withCaseId(CaseContextHolder.currentCaseId()));
|
|
|
|
|
+ Deque<String> queue = pendingMessages.computeIfAbsent(userId, k -> new ArrayDeque<>());
|
|
|
|
|
+ synchronized (queue) {
|
|
|
|
|
+ queue.addLast(msg);
|
|
|
|
|
+ while (queue.size() > REPLAY_BUFFER_MAX) {
|
|
|
|
|
+ queue.pollFirst();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ log.info("用户 {} 没有活跃的 sse 连接,消息已入重放缓冲: {} 条", userId, queue.size());
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("sse 消息缓冲失败: userId={}", userId, e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/** 关闭当前上下文对应用户的 SSE 连接 */
|
|
/** 关闭当前上下文对应用户的 SSE 连接 */
|
|
|
public void closeSee() {
|
|
public void closeSee() {
|
|
|
closeSee(CaseContextHolder.currentUserId());
|
|
closeSee(CaseContextHolder.currentUserId());
|
|
@@ -122,6 +183,8 @@ public class SseService {
|
|
|
if (userId == null) {
|
|
if (userId == null) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
+ // 收口即清缓冲:流程已经结束,历史推送没有重放价值,留着反而会污染下一轮
|
|
|
|
|
+ pendingMessages.remove(userId);
|
|
|
SseEmitter emitter = emitters.get(userId);
|
|
SseEmitter emitter = emitters.get(userId);
|
|
|
if (emitter == null) {
|
|
if (emitter == null) {
|
|
|
return;
|
|
return;
|