How one can know if the client has closed the connection
I've been playing with the new Servlet 3.0 async features with Tomcat 7.0.4. I found this Chat Application, that lets clients hang on GET request to get message updates. This is working just fine when it comes to receiving the mess开发者_Go百科ages.
The problem arises when the client is disconnected i.e. the user closes the browser. It seems that the server does not raise IOException
, even though the client has disconnected. The message thread (see the source code from link above) is happily writing to all stored AsyncContext
's output streams.
Is this a Tomcat bug? or am I missing something here? If this is not a bug, then how I'm supposed to detect whether the client has closed the connection?
The code there at line 44 - 47 is taking care of it,
} catch(IOException ex) {
System.out.println(ex);
queue.remove(ac);
}
And here too at 75 - 83, using timeout thingie,
req.addAsyncListener(new AsyncListener() {
public void onComplete(AsyncEvent event) throws IOException {
queue.remove(ac);
}
public void onTimeout(AsyncEvent event) throws IOException {
queue.remove(ac);
}
});
EDIT: After getting a little more insight.
- Tomcat 7.0.4 is still in beta. So, you can expect such behaviour
- I tried hard but can't find the method
setAsyncTimeout()
in the doc, neither here, nor here. So, I think they dropped it completely in the final version due to some unknown valid reason - The example states, "why should I use the framework instead of waiting for Servlet 3.0 Async API". Which infers that its written before the final thingie
So, what I can say, after combining all these fact, that you are trying to work with the thing that is broken in a sense. That also, may be, the reason for different and weird results.
精彩评论