jersey:如何识别连接何时关闭
如果我有一个长时间等待的请求(例如处理它的线程被阻塞,直到另一个提供一些数据),那么当响应准备好时,客户端可能已经断开连接。有没有办法在处理请求的方法中发现这一点?也就是说,不返回响应并看到jetty/jersey抛出IO异常。
If I have a long waiting request (e.g. the thread handling it is blocked until another provides some data), then by the time the response is ready, the client could have disconnected. Is there a way to discover this in the method that handles the request? That is, not return the response and see jetty/jersey throw IO exceptions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在尝试通过连接发送某些内容并且失败(抛出 IOException )之前,您无法判断连接已关闭。
我正在存储
LinkedBlockingDeque 的地图单例 EJB 中的
(由接收实时流的外部系统的 ID 映射)。当发生某些需要发布到实时提要的事件时,我只需通过该单例 bean 将事件排入队列即可。队列的另一端(如果已连接)是一个 JAX-RS Web 服务,正在等待某个项目变得可用。现在,我使用双端队列而不是队列的原因是,在写入和/或刷新OutputStream
时可能会抛出IOException
,这表明客户端很可能有已断开连接。出队只是让我放回该项目,以便下次外部系统连接时它将成为第一个被取出的项目。You can't tell a connection is down until you try sending something over the connection and it fails (throws
IOException
).I'm storing a map of
LinkedBlockingDeque
s (mapped by the ID of the external system receiving the live stream) in a singleton EJB. When some event occurs that needs to be published to the live feed, I simply enqueue the event via that singleton bean. At the other end of the queue - if connected - is a JAX-RS web service waiting for an item to become available. Now, the reason I use a deque instead of a queue is that anIOException
may be thrown while writing to and/or flushing theOutputStream
, indicating that the client has most probably disconnected. The dequeue simply allows me to put back that item so that it will be the first to be taken the next time that external system connects.