I'm using the Apache CXF Web Services stack. When a client times out or disconnects from the server before the operation is complete, the server keeps running the operation until it is complete. I would like to have the server detect when the client disconnects and handle that accordingly.
Is there a way to detect when a client disconnects using Apache CXF? What about using other Java web-services stacks?
I am not familiar with Apache CXF, but the following should be applicable to any Java Servlet based framework.
In order to determine if a user has disconnected (stop button, closed browser, etc.) the server must attempt to send a packet. If the TCP/IP connection has been closed, an IOException will be thrown.
In theory, a Java application could send a space character at various points during processing. An IOException would signal that the client has gone away and processing can be aborted.
However, there may be a few issues with this technique:
Sending characters during processing will cause the response to be "committed", so it may be impossible to set HTTP headers, cookies, etc. based on the result of the long-running serverside processing.
If the output stream is buffered, the space characters will not be sent immediately, thereby not performing an adequate test. It may be possible to use
flush()as a workaround.It may be difficult to implement this technique for a given framework or view technology (JSP, etc.) For example, the page rendering code will not be able to sent the content type after the response has been committed.
来源:https://stackoverflow.com/questions/48859/detecting-client-disconnects-in-web-services