|
| 1 | +package example.spring.web.sse; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | +import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; |
| 5 | + |
| 6 | +import java.util.Map; |
| 7 | +import java.util.concurrent.ConcurrentHashMap; |
| 8 | +import java.util.concurrent.atomic.AtomicInteger; |
| 9 | +import java.util.function.Consumer; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author <a href="mailto:[email protected]">Zhang Peng</a> |
| 13 | + * @date 2024-04-16 |
| 14 | + */ |
| 15 | +@Slf4j |
| 16 | +public class SseUtil { |
| 17 | + |
| 18 | + public static final long SSE_TIMEOUT = 30000L; |
| 19 | + |
| 20 | + private static final AtomicInteger COUNT = new AtomicInteger(0); |
| 21 | + private static final Map<String, SseEmitter> SSE_MAP = new ConcurrentHashMap<>(); |
| 22 | + |
| 23 | + public static synchronized SseEmitter connect(String key) { |
| 24 | + |
| 25 | + if (SSE_MAP.containsKey(key)) { |
| 26 | + return SSE_MAP.get(key); |
| 27 | + } |
| 28 | + |
| 29 | + try { |
| 30 | + SseEmitter sseEmitter = new SseEmitter(SSE_TIMEOUT); |
| 31 | + sseEmitter.onCompletion(handleCompletion(key)); |
| 32 | + sseEmitter.onError(handleError(key)); |
| 33 | + sseEmitter.onTimeout(handleTimeout(key)); |
| 34 | + SSE_MAP.put(key, sseEmitter); |
| 35 | + COUNT.getAndIncrement(); |
| 36 | + log.info("【SSE】创建连接成功!key: {}, 当前连接数:{}", key, COUNT.get()); |
| 37 | + return sseEmitter; |
| 38 | + } catch (Exception e) { |
| 39 | + log.error("【SSE】创建连接异常!key: {}", key, e); |
| 40 | + return null; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public static synchronized boolean close(String key) { |
| 45 | + SseEmitter sseEmitter = SSE_MAP.get(key); |
| 46 | + if (sseEmitter == null) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + sseEmitter.complete(); |
| 50 | + SSE_MAP.remove(key); |
| 51 | + COUNT.getAndDecrement(); |
| 52 | + log.info("【SSE】key: {} 断开连接!当前连接数:{}", key, COUNT.get()); |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + private static Runnable handleCompletion(String key) { |
| 57 | + return () -> { |
| 58 | + log.info("【SSE】连接结束!key: {}", key); |
| 59 | + close(key); |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + private static Consumer<Throwable> handleError(String key) { |
| 64 | + return t -> { |
| 65 | + log.warn("【SSE】连接异常!key: {}", key, t); |
| 66 | + close(key); |
| 67 | + }; |
| 68 | + } |
| 69 | + |
| 70 | + private static Runnable handleTimeout(String key) { |
| 71 | + return () -> { |
| 72 | + log.info("【SSE】连接超时!key: {}", key); |
| 73 | + close(key); |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + public static void send(String key, Object message) { |
| 78 | + if (SSE_MAP.containsKey(key)) { |
| 79 | + try { |
| 80 | + SseEmitter sseEmitter = SSE_MAP.get(key); |
| 81 | + sseEmitter.send(message); |
| 82 | + } catch (Exception e) { |
| 83 | + log.error("【SSE】发送消息异常!key: {}, message: {}", key, message, e); |
| 84 | + close(key); |
| 85 | + } |
| 86 | + } else { |
| 87 | + log.warn("【SSE】发送消息失败!key: {}, message: {}", key, message); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments