Servlet 3.0 测试查询中的异步属性

Asynchronous property in Servlet 3.0 testing query

我已经使用以下教程在 Servlet 3.0 中实现了异步 属性。

http://hmkcode.com/java-servlet-3-0-asynchronous-support/

在后端实现可运行 class 后,我观察到创建了 2 个线程,一个以异步方式结束,另一个进行后端处理。我能够成功实现提到的异步 属性。 在 runnable class 中,我保持了 25 秒的睡眠,并且我尝试在 servlet class 中使用 outStream.println(Calendar.getInstance().getTimeInMillis()),我已经观察到 println 中打印的 deviation.The 时间值是请求开始的时间,但 outStream 在 25 秒后打印在 URL 命中页面上。 我只是想了解打印是在 servlet 中构建的(基于我进行此分析的时间戳),为什么它在工作人员 class 睡眠时间后打印在 servlet URL 命中页面中。

 @WebServlet(name="asyncServlet",value = {"/async"},asyncSupported = true)       
public class AsyncServlet extends HttpServlet 
{
private static final long serialVersionUID = 1L;
@Override
protected void handleRequest(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
servletoutputstream out = response.getoutputstream(); 
   final AsyncContext ctx = req.startAsync();
 ctx.addListener(new AsyncListener() {
        @Override
        public void onTimeout(AsyncEvent arg0) throws IOException {
            System.out.println("onTimeout...");             
        }

        @Override
        public void onStartAsync(AsyncEvent arg0) throws IOException {
            System.out.println("onStartAsync...");              
        }

        @Override
        public void onError(AsyncEvent arg0) throws IOException {
            System.out.println("onError...");           
        }

        @Override
        public void onComplete(AsyncEvent arg0) throws IOException {
            System.out.println("onComplete...");
        }
   });
   ctx.start(new Runnable() {
        @Override
        public void run() {
             try {
        Thread.currentThread.sleep(1000);

            } catch InterruptedException e) {
                e.printStackTrace();
            }

         ctx.complete();
        }
   });
    out.write("Got the request"+calendar.getinstance().gettimeinmillis());
out.close();
}
}

我这里是打印出来的,out字符串中抓取的时间是在休眠时间之前,打印出来是在休眠时间之后。

我已尝试使用 PrintWriter,但观察到相同的输出。 有什么方法可以使用上面的代码在睡眠时间之前打印响应。

下面是您想要的代码。

     @WebServlet(name = "asyncServlet", value = { "/async" }, asyncSupported = true)
public class AsyncServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
    final   PrintWriter out = response.getWriter(); 

        final AsyncContext ctx = req.startAsync();
        ctx.addListener(new AsyncListener() {
            @Override
            public void onTimeout(AsyncEvent arg0) throws IOException {
                System.out.println("onTimeout...");             
            }

            @Override
            public void onStartAsync(AsyncEvent arg0) throws IOException {
                System.out.println("onStartAsync...");              
            }

            @Override
            public void onError(AsyncEvent arg0) throws IOException {
                System.out.println("onError...");           
            }

            @Override
            public void onComplete(AsyncEvent arg0) throws IOException {
                System.out.println("onComplete...");
                out.close();
            }
        });
        ctx.start(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                }catch(InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    out.print("<br> Got the request : "+Calendar.getInstance().getTimeInMillis()+" For Async thread :"+Thread.currentThread().getName());
                    out.flush();

                } catch (Exception e) {
                    e.printStackTrace();
                }
                ctx.complete();
            }
        });
        try {
            long time=Calendar.getInstance().getTimeInMillis();
            out.print("<br>Got the request :"+time+" For Original thread completed :"+Thread.currentThread().getName());
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

输出:

Got the request :1426238802101 For Original thread completed :http-bio-8080-exec-14

3 秒后:

 Got the request : 1426238805101 For Async thread :http-bio-8080-exec-9

Click here查看包含异步Servlet详细代码的演示项目。