标签:
/**
* Servlet implementation class MainServlet
*/
@WebServlet(value = "/MainServlet", asyncSupported = true)
public class AsynServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public AsynServlet() {
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
long startTime = System.currentTimeMillis();
System.out.println("DoGet Start ThreadName = " + Thread.currentThread().getName() + " ThreadId = "
+ Thread.currentThread().getId());
AsyncContext asyncCtx = request.startAsync();
asyncCtx.setTimeout(9000);
ExecutorService executorService = (ExecutorService) request.getServletContext().getAttribute("executorService");
executorService.execute(() -> {
try {
System.out.println();
Thread.sleep(5000);
asyncCtx.complete();
} catch (Exception e) {
e.printStackTrace();
}
});
long endTime = System.currentTimeMillis();
System.out.println("doGet End ThreadName = " + Thread.currentThread().getName() + " ThreadId = "
+ Thread.currentThread().getId() + " Time = " + (endTime - startTime) + " ms.");
request.getRequestDispatcher("/AnotherServlet").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
Other - 02 - Servlet学习笔记 - 异步Servlet
标签:
原文地址:http://www.cnblogs.com/yaowu/p/67ba496f23047ed2fd4728bdc706fec3.html