Detecting Client Disconnects in Web Services

大憨熊 提交于 2019-12-07 04:39:09

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/48859/detecting-client-disconnects-in-web-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!