【发布时间】:2015-03-12 09:40:29
【问题描述】:
我已经使用下面的教程在 Servlet 3.0 中实现了异步属性。
http://hmkcode.com/java-servlet-3-0-asynchronous-support/
在后端实现一个可运行类后,我观察到创建了 2 个线程,其中一个以异步方式结束,另一个进行后端处理。我能够成功实现提到的异步属性。 在可运行类中,我保持了 25 秒的睡眠,我尝试在 servlet 类中使用 outStream.println(Calendar.getInstance().getTimeInMillis()),我观察到了偏差。打印的时间值println 是请求开始但 25 秒后在 URL 命中页面上打印 outStream 的时间。 我只是想了解何时在 servlet 中构建打印(基于我进行此分析的时间戳),为什么它会在工人类睡眠时间后打印在 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();
}
}
这里我打印出来,输出字符串中捕获的时间是在睡眠时间之前,但它是在睡眠时间之后打印的。
我已尝试使用 PrintWriter,但仍观察到相同的输出。 有没有办法在睡眠时间之前使用上面的代码打印响应。
【问题讨论】:
-
你能发布你的代码吗?
-
伙计们......请有人帮忙,我无法使用上述代码以异步方式将响应发送回服务器。座右铭是异步发送响应。
标签: java multithreading jsp servlets asynchronous