检测Web服务中的客户端断开连接 [英] Detecting Client Disconnects in Web Services

查看:160
本文介绍了检测Web服务中的客户端断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apache CXF Web服务堆栈。当客户端在操作完成之前超时或断开与服务器的连接时,服务器将继续运行该操作,直到完成为止。我希望服务器检测到客户端何时断开连接并相应地处理它。

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.

有没有办法检测客户端何时使用Apache CXF断开连接?那么使用其他Java Web服务堆栈呢?

Is there a way to detect when a client disconnects using Apache CXF? What about using other Java web-services stacks?

推荐答案

我不熟悉 Apache CXF ,但以下内容应适用于任何基于Java Servlet的框架。

I am not familiar with Apache CXF, but the following should be applicable to any Java Servlet based framework.

为了确定用户是否已断开连接(停止按钮,关闭浏览器等),服务器必须尝试发送数据包。如果TCP / IP连接已关闭,将抛出 IOException

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.

理论上,Java应用程序可以在处理过程中在各个点发送空格字符。 IOException 将表示客户端已经离开并且处理可以中止。

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. 在处理过程中发送字符会导致响应被提交,因此可能无法实现根据长时间运行的服务器端处理的结果设置HTTP标头,cookie等。

  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.

如果输出流被缓冲,空格字符将不能立即发送,因此不能进行充分的测试。可以使用 flush()作为解决方法。

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.

可能难以实现这种技术适用于给定的框架或视图技术(JSP等)例如,页面呈现代码将无法在提交响应后发送内容类型。

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.

这篇关于检测Web服务中的客户端断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆