超时后的JavaEE 6 AsyncContext行为 [英] JavaEE 6 AsyncContext behavior after timeout

查看:306
本文介绍了超时后的JavaEE 6 AsyncContext行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是AsyncContext的超时之后的确切行为?是否仍然可以运行,它返回使用的GetResponse()?响应


解决方案

的Java Servlet 3.0 第34页维护版本内容如下:


  

这个方法的调用 - ServletRequest.startAsync(ServletRequest中REQ,
  ServletResponse的水库)
- 确保响应没有承诺何时
  在应用程序退出了该服务的方法。它承诺时
  AsyncContext.complete被称为对返回的AsyncContext或
  AsyncContext超时,有没有相关的处理听众
  超时。对于异步超时定时器不会开始直到
  要求和它的相关联的响应已经从容器返回。
  的AsyncContext可以用来写入从异步响应
  线。它也可以用来只通知响应不
  封闭和承诺。


的javax.servlet.AsyncContext接口的javadoc


  

在的情况下,一个异步操作已超时,则
  容器必须通过这些步骤执行:


  
  

      
  1. 调用,在他们onTimeout方法,所有实例AsyncListener在ServletRequest中登记在其上异步操作
      已启动。

  2.   
  3. 如果没有听众称为完全()或派遣()方法,执行与状态code等于错误调度
      到HttpServletResponse.SC_INTERNAL_SERVER_ERROR。

  4.   (
  5. 如果没有找到匹配的错误页面,或错误页面没有调用完成()或任何派遣()方法调用完成)。

  6.   

上面的最后一行是最重要的 - 有会是 AsyncContext.complete()的调用的javadoc中说


  

完成对已启动对请求的异步操作
  曾用于initialze此AsyncContext,关闭响应
  这是用于初始化该AsyncContext。


使用 AsyncContext 关闭,调用它的大多数方法将抛出 IllegalStateException异常

What is the exact behavior of AsyncContext after it times out? Does it still run and does it return response using getResponse()?

解决方案

Java Servlet 3.0 Maintenance Release on page 34 reads:

A call to this method - ServletRequest.startAsync(ServletRequest req, ServletResponse res) - ensures that the response isn't committed when the application exits out of the service method. It is committed when AsyncContext.complete is called on the returned AsyncContext or the AsyncContext times out and there are no listeners associated to handle the time out. The timer for async time outs will not start until the request and it’s associated response have returned from the container. The AsyncContext could be used to write to the response from the async thread. It can also be used to just notify that the response is not closed and committed.

From the javadoc of the javax.servlet.AsyncContext interface:

In the event that an asynchronous operation has timed out, the container must run through these steps:

  1. Invoke, at their onTimeout method, all AsyncListener instances registered with the ServletRequest on which the asynchronous operation was initiated.
  2. If none of the listeners called complete() or any of the dispatch() methods, perform an error dispatch with a status code equal to HttpServletResponse.SC_INTERNAL_SERVER_ERROR.
  3. If no matching error page was found, or the error page did not call complete() or any of the dispatch() methods, call complete()."

The last line above is of most importance - there'll be a call to AsyncContext.complete(). The javadoc says:

Completes the asynchronous operation that was started on the request that was used to initialze this AsyncContext, closing the response that was used to initialize this AsyncContext.

With AsyncContext closed, calls to most of its methods will throw IllegalStateException.

这篇关于超时后的JavaEE 6 AsyncContext行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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