Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Client Disconnects in Web Services

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?

like image 469
Chris Dail Avatar asked Sep 07 '08 22:09

Chris Dail


People also ask

How do you determine if a TCP client has been disconnected?

In TCP there is only one way to detect an orderly disconnect, and that is by getting zero as a return value from read()/recv()/recvXXX() when reading. There is also only one reliable way to detect a broken connection: by writing to it.


1 Answers

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:

  1. 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.

  2. 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.

  3. 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.

like image 129
John Vasileff Avatar answered Sep 18 '22 20:09

John Vasileff