Java Server-Sent Events没有数据

问题:做可视化大屏,开始有数据,刷新几次页面之后就没数据了很奇怪,前端的请求在不停的重新连接。

解决方案:因为之前的代码中使用了单个 SseEmitter 实例,这导致旧的连接没有正确关闭而产生冲突。可以通过为每个请求创建一个新的 SseEmitter 实例来优化代码,同时确保旧的实例被适当地清理。

示例:

import com.alibaba.fastjson.JSONObject;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import xin.admin.domain.sse.NotificationSSE;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/admin/homePage")
public class NotificationSSEController {

    // 线程池,让sse异步操作
    private final ExecutorService executorService = Executors.newCachedThreadPool();

    // 使用Map来存储每个客户端的SseEmitter
    private final Map<String, SseEmitter> emitters = new ConcurrentHashMap<>();

    // 要发送的数据
    public static NotificationSSE data = new NotificationSSE();

    @CrossOrigin
    @RequestMapping(value = "/notification", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter stream(HttpServletRequest request) {
        String clientId = getClientId(request);
        SseEmitter emitter = new SseEmitter(24L * 60 * 60 * 1000);

        emitters.put(clientId, emitter);

        emitter.onCompletion(() -> emitters.remove(clientId));
        emitter.onTimeout(() -> emitters.remove(clientId));
        emitter.onError((e) -> emitters.remove(clientId));

        sendNotification(); // 当客户端连接时立即发送通知
        return emitter;
    }

    @Scheduled(fixedRate = 1000 * 60 * 10)
    public void heartbeat() {
        sendNotification();
    }

    // B函数:负责SSE发送
    public void sendNotification() {
        emitters.forEach((clientId, emitter) -> {
            executorService.execute(() -> {
                try {
                    emitter.send(SseEmitter.event()
                            .id(String.valueOf(System.currentTimeMillis()))
                            .data(JSONObject.toJSONString(data)));
                } catch (Exception e) {
                    emitter.completeWithError(e);
                }
            });
        });
    }

    // 生成或获取客户端ID
    private String getClientId(HttpServletRequest request) {
        // 这里可以根据实际情况生成唯一的客户端ID
        return request.getSession(true).getId();
    }
}