服务器端实时推送技术之SseEmitter的用法

时间:2023-01-26 03:37:31

这是SpringMVC提供的一种技术,可以实现服务端向客户端实时推送数据.用法非常简单,只需要在Controller提供一个接口,创建并返回SseEmitter对象,发送数据可以在另一个接口调用其send方法发送数据.这个SseEmitter对象和DeferredResult有异曲同工之妙,只是SseEmitter可以在一次请求中返回多条数据,而DeferredResult只能返回一条.关于DeferredResult的用法请求参考我的另一篇博客:https://www.cnblogs.com/jun1019/p/10807055.html

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; /**
* 服务器端实时推送技术之 SseEmitter 的用法测试
* <p>
* 测试步骤:
* 1.请求http://localhost:8888/sse/start?clientId=111接口,浏览器会阻塞,等待服务器返回结果;
* 2.请求http://localhost:8888/sse/send?clientId=111接口,可以请求多次,并观察第1步的浏览器返回结果;
* 3.请求http://localhost:8888/sse/end?clientId=111接口结束某个请求,第1步的浏览器将结束阻塞;
* 其中clientId代表请求的唯一标志;
*
* @author syj
*/
@RestController
@RequestMapping("/sse")
public class SseEmitterController {
private static final Logger logger = LoggerFactory.getLogger(SseEmitterController.class); // 用于保存每个请求对应的 SseEmitter
private Map<String, Result> sseEmitterMap = new ConcurrentHashMap<>(); /**
* 返回SseEmitter对象
*
* @param clientId
* @return
*/
@RequestMapping("/start")
public SseEmitter testSseEmitter(String clientId) {
// 默认30秒超时,设置为0L则永不超时
SseEmitter sseEmitter = new SseEmitter(0L);
sseEmitterMap.put(clientId, new Result(clientId, System.currentTimeMillis(), sseEmitter));
return sseEmitter;
} /**
* 向SseEmitter对象发送数据
*
* @param clientId
* @return
*/
@RequestMapping("/send")
public String setSseEmitter(String clientId) {
try {
Result result = sseEmitterMap.get(clientId);
if (result != null && result.sseEmitter != null) {
long timestamp = System.currentTimeMillis();
result.sseEmitter.send(timestamp);
}
} catch (IOException e) {
logger.error("IOException!", e);
return "error";
} return "Succeed!";
} /**
* 将SseEmitter对象设置成完成
*
* @param clientId
* @return
*/
@RequestMapping("/end")
public String completeSseEmitter(String clientId) {
Result result = sseEmitterMap.get(clientId);
if (result != null) {
sseEmitterMap.remove(clientId);
result.sseEmitter.complete();
}
return "Succeed!";
} private class Result {
public String clientId;
public long timestamp;
public SseEmitter sseEmitter; public Result(String clientId, long timestamp, SseEmitter sseEmitter) {
this.clientId = clientId;
this.timestamp = timestamp;
this.sseEmitter = sseEmitter;
}
}
}

这个技术有什么用呢? 就看你的想象力了, 比如可以用于和进度有关的操作: 安装进度, 部署进度, 任务执行进度等等.