I'm trying to establish an SSE communication channel between a Jakarta EE server and other web or Java clients. In order to send messages to such clients, I keep a list of them as internal server state. The problem is, I can't find a way to detect when the clients close the connection, so that I can free up the corresponding server resources.
Here is more or less how I am establishing the connection:
@ApplicationScoped
@Path("/sse")
public class SseController {
private Sse sse;
private List<SseEventSink> sinks = new CopyOnWriteArrayList<>();
@GET
@Produces(SERVER_SENT_EVENTS)
public void onSse(@Context Sse sse, @Context SseEventSink sink) {
this.sse = sse;
this.sinks.add(sink);
}
}
When is one supposed to clean up a closed client? From my tests, the server remains unaware of a client closure for as long as I've been willing to wait (~15 minutes), unless the server attempts to send three messages to the closed client; then somehow, after three send attempts, the third one fails, which gives an opportunity for cleaning up. I find this far from optimal.
If it helps, I am using OpenLiberty, which I believe uses RESTEasy as its SSE implementation.