Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
poc-api
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
poc
poc-api
Commits
d445f487
Commit
d445f487
authored
Dec 25, 2025
by
alex yao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SSEUtil工具优化
parent
afd2cb4c
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
58 additions
and
26 deletions
+58
-26
SSEUtil.java
src/main/java/cn/com/poc/common/utils/SSEUtil.java
+58
-26
No files found.
src/main/java/cn/com/poc/common/utils/SSEUtil.java
View file @
d445f487
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.http.HttpServletResponse
;
import
java.io.IOException
;
import
java.nio.charset.Charset
;
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
* @date 2025/3/4
*/
public
class
SSEUtil
{
public
class
SSEUtil
implements
AutoCloseable
{
private
final
Logger
logger
=
LoggerFactory
.
getLogger
(
SSEUtil
.
class
);
private
final
ServletOutputStream
outputStream
;
private
final
Object
lock
=
new
Object
();
// 添加锁对象
private
volatile
boolean
closed
=
false
;
// 添加关闭状态标记
public
SSEUtil
()
throws
IOException
{
ServletRequestAttributes
servletRequestAttributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
ServletRequestAttributes
servletRequestAttributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletResponse
response
=
servletRequestAttributes
.
getResponse
();
if
(
response
==
null
)
{
throw
new
IOException
(
"无法获取HttpServletResponse"
);
...
...
@@ -37,12 +41,27 @@ public class SSEUtil {
}
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"
;
outputStream
.
write
(
message
.
getBytes
(
StandardCharsets
.
UTF_8
));
outputStream
.
flush
();
}
}
public
boolean
complete
()
{
if
(
closed
)
{
return
true
;
// 已经关闭,返回true
}
synchronized
(
lock
)
{
if
(
closed
)
{
return
true
;
}
try
{
outputStream
.
close
();
}
catch
(
IOException
e
)
{
...
...
@@ -51,8 +70,16 @@ public class SSEUtil {
}
return
true
;
}
}
public
boolean
completeByError
(
String
errorMsg
)
{
if
(
closed
)
{
return
true
;
// 已经关闭,返回true
}
synchronized
(
lock
)
{
if
(
closed
)
{
return
true
;
}
try
{
String
mess
=
"data: "
+
errorMsg
+
"\n\n"
;
outputStream
.
write
(
mess
.
getBytes
(
StandardCharsets
.
UTF_8
));
...
...
@@ -64,5 +91,10 @@ public class SSEUtil {
}
return
true
;
}
}
@Override
public
void
close
()
throws
Exception
{
complete
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment