2013-12-09 59 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對象引用的方法。