gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:08:41 25 4
gpt4 key购买 nike

我使用下面的教程在 Servlet 3.0 中实现了异步属性。

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

在后端实现一个可运行类后,我观察到创建了 2 个线程,其中一个以异步方式结束,另一个执行后端处理。我能够成功实现提到的异步属性。在runnable类中,我保持了25秒的 sleep ,并且尝试在servlet类中使用outStream.println(Calendar.getInstance().getTimeInMillis()),我观察到了偏差。println中打印的时间值是请求开始时的时间,但outStream是在25秒后打印在URL命中页面上的。我只是想了解何时在 servlet 中构建打印(基于我进行此分析的时间戳),为什么它在工作类 sleep 时间之后打印在 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();
}
}

这里我打印出来,输出字符串中捕获的时间是在 sleep 时间之前,但它是在 sleep 时间之后打印的。

我尝试过使用 PrintWriter,但观察到相同的输出。有没有办法使用上面的代码在 sleep 时间之前打印响应。

最佳答案

下面是可以按照您的要求工作的代码。

     @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 的详细代码。

关于java - Servlet 3.0 测试查询中的异步属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29006256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com