Commit d445f487 authored by alex yao's avatar alex yao

SSEUtil工具优化

parent afd2cb4c
package cn.com.poc.common.utils; package cn.com.poc.common.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/** /**
* @author alex.yao * @author alex.yao
* @date 2025/3/4 * @date 2025/3/4
*/ */
public class SSEUtil { public class SSEUtil implements AutoCloseable {
private final Logger logger = LoggerFactory.getLogger(SSEUtil.class); private final Logger logger = LoggerFactory.getLogger(SSEUtil.class);
private final ServletOutputStream outputStream; private final ServletOutputStream outputStream;
private final Object lock = new Object(); // 添加锁对象
private volatile boolean closed = false; // 添加关闭状态标记
public SSEUtil() throws IOException { public SSEUtil() throws IOException {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ServletRequestAttributes servletRequestAttributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = servletRequestAttributes.getResponse(); HttpServletResponse response = servletRequestAttributes.getResponse();
if (response == null) { if (response == null) {
throw new IOException("无法获取HttpServletResponse"); throw new IOException("无法获取HttpServletResponse");
...@@ -37,12 +41,27 @@ public class SSEUtil { ...@@ -37,12 +41,27 @@ public class SSEUtil {
} }
public void send(String data) throws IOException { public void send(String data) throws IOException {
if (closed) {
throw new IOException("SSE连接已关闭");
}
synchronized (lock) {
if (closed) {
throw new IOException("SSE连接已关闭");
}
String message = "data: " + data + "\n\n"; String message = "data: " + data + "\n\n";
outputStream.write(message.getBytes(StandardCharsets.UTF_8)); outputStream.write(message.getBytes(StandardCharsets.UTF_8));
outputStream.flush(); outputStream.flush();
} }
}
public boolean complete() { public boolean complete() {
if (closed) {
return true; // 已经关闭,返回true
}
synchronized (lock) {
if (closed) {
return true;
}
try { try {
outputStream.close(); outputStream.close();
} catch (IOException e) { } catch (IOException e) {
...@@ -51,8 +70,16 @@ public class SSEUtil { ...@@ -51,8 +70,16 @@ public class SSEUtil {
} }
return true; return true;
} }
}
public boolean completeByError(String errorMsg) { public boolean completeByError(String errorMsg) {
if (closed) {
return true; // 已经关闭,返回true
}
synchronized (lock) {
if (closed) {
return true;
}
try { try {
String mess = "data: " + errorMsg + "\n\n"; String mess = "data: " + errorMsg + "\n\n";
outputStream.write(mess.getBytes(StandardCharsets.UTF_8)); outputStream.write(mess.getBytes(StandardCharsets.UTF_8));
...@@ -64,5 +91,10 @@ public class SSEUtil { ...@@ -64,5 +91,10 @@ public class SSEUtil {
} }
return true; return true;
} }
}
@Override
public void close() throws Exception {
complete();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment