我有一个使用 Angular 5 前端使用的 Spring 开发的 REST API。我的服务大约需要 5 分钟才能完成,并且我的负载均衡器会断开来自浏览器的连接。
我们无法实现 WebSocket,因为 ws 协议(protocol)被代理阻止了。我们想在服务器端实现 Spring SSE,在客户端实现 Angular 上的 EventSource。
我们有以下客户端和服务器代码。
当异步处理完成并且服务器尝试发送响应时,我们收到以下错误。我无法理解这是 Tomcat 断开连接还是浏览器?
17:32:40.101 [GithubLookup-1] INFO o.a.coyote.http11.Http11Processor - An error occurred in processing while on a non-container thread. The connection will be closed immediately
java.io.IOException: An established connection was aborted by the software in your host machine
at sun.nio.ch.SocketDispatcher.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
代码
public SseEmitter doSomething() {
final SseEmitter emitter = new SseEmitter(600000L);
CompletableFuture<Response> res = ... some Service call which takes 5 min, but annotated with @Async
completableFuture.whenComplete((res, ex) -> {
emitter.send(res);
emitter.complete();
}
emitter.send("Running"); // Some fake status to let client know it is accepted and running
return emitter;
}
在客户端我们使用事件源
let eventSource = new EventSource(url, {
xhrHeaders: {
'Content-Type': 'text/event-stream'
}
});
eventSource.onmessage = (event) => {
console.log('Received event: ', event);
};
最佳答案
连接保持事件不起作用,因为连接上没有事件。
我的负载均衡器正在终止连接。
现在通过每 30 秒在连接上发送一些虚假数据并保持连接事件来解决此问题。
关于用于长时间运行流程的 Spring SSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49505850/