Reputation: 71
I have a REST API developed using Spring which is used by Angular 5 frontend. My service takes around 5 minutes to complete, and my load-balancer disconnects the connection coming from browser.
We cant implement WebSocket as ws protocol is blocked by the proxy. We want to implement Spring SSE on server side and EventSource on angular as a client.
We have below client and server code.
When the async processing completes and server tries to send response we are getting below error. I am not able to understand whether this is Tomcat snapping the connection or the browser?
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)
Code
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;
}
On client side we are using eventsource
let eventSource = new EventSource(url, {
xhrHeaders: {
'Content-Type': 'text/event-stream'
}
});
eventSource.onmessage = (event) => {
console.log('Received event: ', event);
};
Upvotes: 4
Views: 2389
Reputation: 71
Connection keep alive is not working because there is no activity on the connection.
My load balancer was terminating the connection.
This is now resolved by sending some fake data on connection every 30Sec and keeping the connection alive.
Upvotes: 1