<servlet>
<description>xxx</description>
<servlet-name>xxx</servlet-name>
<servlet-class>com.xxx.yyy</servlet-class>
<async-supported>true</async-supported>
</servlet>
Run Code Online (Sandbox Code Playgroud)
async-supportedservlet的web.xml配置文件的目的是什么?我可以在什么情况下使用它?
我正在使用异步Servlet来处理请求,
根据Docs :( complete(),dispatch())
????????????????????????????????????????????????????????????????????????????????
? void complete() ? Completes the asynchronous operation and closes the ?
? ? response associated with this asynchronous context. ?
? ? You call this method after writing to the response object ?
? ? inside the asynchronous context. ?
????????????????????????????????????????????????????????????????????????????????
? void dispatch() ? Dispatches the request and response objects ?
? ? of this AsyncContext to the servlet container. ?
????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)
我无法理解async.dispatch(),async.complete()及其工作原理.
我对此几乎没有疑问:
async.dispatch() …我已经尝试写入响应,因为没有正确的断开回调:
private boolean write(byte[] output, AsyncContext context) {
try {
OutputStream stream = context.getResponse().getOutputStream();
stream.write(output);
stream.flush();
return true;
} catch (IOException ex) {
//client disconnected
log.error(ex);
removeAsyncContext(context);
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
但这似乎没有诀窍.当客户端断开连接时,写入和刷新缓冲区不会引发异常.
奇怪的是,第二次尝试写入输出流时(断开连接后),写入会引发异常.它看起来像你第一次写/刷它,一些内部状态被设置为错误,没有通知.
我试过Jetty 8和Tomcat 7,我看到了同样的行为.
是否有解决方案来确定客户是否收到了该消息?我错过了什么吗?
我正在使用 Servlet 3.0 的javax.servlet.AsyncContext接口实现服务器发送的事件。
但是,我无法理解我应该如何处理对等断开连接等 I/O 错误。
对于给定的AsyncContext ac = request.startAsync(),我可以调用ac.getResponse().getWriter().print(something)然后ac.getResponse.getWriter().flush()它工作正常。但是,当客户端断开连接时,我不会收到错误消息——即使我附加了一个侦听器,onError也不会调用它的方法。
我用 Jetty 8 和 Tomcat 7 对它进行了测试,似乎与客户端的断开连接没有报告给应用程序。
如何检测通信错误?
asynchronous ×3
java ×3
servlet-3.0 ×2
servlets ×2
client ×1
comet ×1
concurrency ×1
disconnect ×1
guava ×1
jakarta-ee ×1
jetty ×1
tomcat ×1
web.xml ×1