2013-12-09 35 views
1

我在计算异步servlet流如何异步行为。servlet 3.0异步流如何表现

如果有servlet Servlet_1,并且我们从servlet调用一个实用工具类SampleUtility的util方法,并在util方法中进行某种操作后向Servlet返回一些值。

因此,对于流动异步我们需要asyncContext传递给UTIL方法,或者干脆由语句

AsyncContext asyncContext = request.startAsync(); 

开始asynccontext将有足够的流动是异步的?

回答

1

对于Servlet是异步的3个步骤需要必须执行。

  1. Annote Web servlet的注释为

    @WebServlet(urlPatterns={“/servletexample”},aysncSupported=true)

    其标记asyncSupported true将使能异步流程。

  2. 通过下面的语句启动AsyncContext

    AsyncContext ac=request.startAsync();

  3. 最后落实startAsync()

    asyncContext.start(new Runnable(){ public void run(){ //Write the non-blocking code here } }

0

将足以使流程异步?

不,这还不够。

@WebServlet(urlPatterns={"/asyncservlet"}, asyncSupported=true) 
    public class AsyncServlet extends HttpServlet { ... } 

有关详细信息,请通过这个tutorial:要在一个servlet启用异步处理,对@WebServlet注解如下参数asyncSupported设置为true

+1

据我所知,设置参数asyncSupported为true是必要的,但我在问流量 –

0

AsyncContext.forward(path)AsyncContext.forward()将请求转发回容器,以便您可以使用像JSP这样的框架生成响应。因此,您需要将asyncContext传递给该方法,因为AsyncContext提供了获取ServletRequestServletResponse对象引用的方法。