How can I find out if the client closed the connection

I played with the new async features for Servlet 3.0 with Tomcat 7.0.4. I found this chat application that allows clients to hang in a GET request to receive message updates. This works great when it comes to receiving messages.

The problem occurs when the client disconnects, that is, the user closes the browser. It seems that the server is not raising IOException, even if the client has disconnected. The message stream (see the source code from the link above) is happy to write all saved output streams AsyncContext.

Is this a Tomcat bug? or am I missing something? If this is not an error, then how should I determine if the client has closed the connection?

+5
source share
1 answer

The code on line 44-47 takes care of it,

} catch(IOException ex) {
    System.out.println(ex);
    queue.remove(ac);
}

And here, too, at 75 - 83, using a timeout,

req.addAsyncListener(new AsyncListener() {
    public void onComplete(AsyncEvent event) throws IOException {
        queue.remove(ac);
    }

    public void onTimeout(AsyncEvent event) throws IOException {
        queue.remove(ac);
    }
});

The EDIT: . Having received a little more information.

  • Tomcat 7.0.4 is still in beta. So you can expect this behavior.
  • I tried, but I can’t find the method setAsyncTimeout()in the document here and here . So, I think that they completely dropped it in the final version due to an unknown reliable reason.
  • The example says: "Why should I use the framework instead of waiting for the Async API for Servlet 3.0." Which reports that it is written before the finale.

, , , , . .

+1

All Articles