AsyncContext和I / O错误处理(当对等连接断开时) [英] AsyncContext and I/O error handling (when peer disconnects)

查看:304
本文介绍了AsyncContext和I / O错误处理(当对等连接断开时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Servlet 3.0的 javax.servlet.AsyncContext 接口。

I'm implementing Server-Sent Events using Servlet 3.0's javax.servlet.AsyncContext interface.

但是我无法理解如何处理像对等断开连接这样的I / O错误。

However I can't understand how I should handle I/O errors like peer disconnect.

对于给定的 AsyncContext ac = request.startAsync(),我可以调用 ac.getResponse()。 getWriter()。print(something)然后 ac.getResponse.getWriter()。flush(),它运行正常。但是,当客户端断开连接时,我不会收到错误 - 即使我附加了一个监听器,也不会调用它的 onError 方法。

For a given AsyncContext ac = request.startAsync(), I can call ac.getResponse().getWriter().print(something) and then ac.getResponse.getWriter().flush() and it works fine. However when a client disconnects, I don't get an error - even if I attach a listener, its onError method is not called.

我用Jetty 8和Tomcat 7测试了它,似乎与客户端的断开连接没有报告给应用程序。

I tested it with both Jetty 8 and Tomcat 7 and it seems that the disconnect from the client is not reported back to the application.

什么可以是为了检测通信错误?

What can be done to detect a communication error?

推荐答案

问题在于: ac.getResponse.getWriter( ).flush()不抛出 IOException

所以为了得到一个I / O操作时的错误通知您需要使用 ServletOutputStream

So in order to get a error notification upon I/O operation you need to use ServletOutputStream instead:

try {
   ServletOutputStream out = ac.getResponse().getOutputStream();
   out.print(stuff);
   out.flush(); // throws IOException
}
catch(IOException e) {
   // handle a error
}

这篇关于AsyncContext和I / O错误处理(当对等连接断开时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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