refactor(message): 优化SSE会话注册表的容器关闭事件处理

- 移除@PreDestroy注解
- 添加ContextClosedEvent事件监听器
- 使用Spring事件机制替代JSR-250生命周期回调
- 提高容器关闭时SSE连接清理的可靠性
- 增强代码的Spring框架集成一致性
This commit is contained in:
theo
2026-04-03 09:49:33 +08:00
parent 1635f976c3
commit b411baaddd

View File

@@ -1,8 +1,9 @@
package com.youlai.boot.message.registry; package com.youlai.boot.message.registry;
import com.youlai.boot.message.dto.OnlineUserDTO; import com.youlai.boot.message.dto.OnlineUserDTO;
import jakarta.annotation.PreDestroy;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
@@ -198,7 +199,7 @@ public class SseSessionRegistry {
/** /**
* 容器关闭时主动断开所有 SSE 连接,避免阻塞应用停止 * 容器关闭时主动断开所有 SSE 连接,避免阻塞应用停止
*/ */
@PreDestroy @EventListener(ContextClosedEvent.class)
public void destroy() { public void destroy() {
int count = emitterUserMap.size(); int count = emitterUserMap.size();
if (count == 0) { if (count == 0) {