AsyncContext Example
In this section, you will learn about Servlet 3.0 AsyncContext using a complete example.
Need of AsyncContext & startAsync() method
Often during handling request, your application thread is waiting for some external resource due to which it become idle for some time. Due to this, you are engaging the thread and therefore a lot of memory is occupied by you without doing any function.
Consider a situation where your application is providing the downloading of files with limited output. In this case, your threads are idle most of the time since they are awaiting to send next bundle of data. Prior to Servlet 3.0 , you couldn't attend/process more than your HTTP thread limit.
With Servlet 3.0, you can attend/ process thousands of connections concurrently which is much more than thread limit. It means you can connect to thousands of client with few HTTP threads.
Method startAsync() doesn't create any thread. Means it will not create new thread for every async request. It just tells the Servlet container do not close this request until in tell you to do so.
Tools & technology used :
- Eclipse Helios 3.6.1
- Tomcat 7
- jdk1.6.0_18
EXAMPLE
In the below example, you will learn how to initialize a AsyncContext using ServletRequest object and dispatches the request and response objects of the AsyncContext to a given URL.
AsyncContextDispatchMethod.java
package roseindia; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import javax.servlet.AsyncContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(urlPatterns = "/AsyncContextDispatch", asyncSupported = true) public class AsyncContextDispatchMethod extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Date date = new Date(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("<h2>AsyncContext Example </h2>"); request.setAttribute("receivedAt", date); out.println("Request Time :" + request.getAttribute("receivedAt")); AsyncContext asyncCtx = request.startAsync(); ServletRequest req = asyncCtx.getRequest(); boolean bol = req.isAsyncStarted(); // This will return true out.println("<br>AsyncStarted : " + bol); if (bol) { asyncCtx.dispatch("/asyncOutput.html"); } boolean bol1 = req.isAsyncStarted(); // This will return false out.println("<br>AsyncStarted : " + bol1); boolean bol2 = req.isAsyncSupported(); // This will return true out.println("<br>AsyncSupported : " + bol2); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
asyncOutput.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Asynchronous Servlet 3.0 Example</title> </head> <body> <h3>Given above the Servlet 3.0 AsyncContext Interface Dispatch() Method example</h3> </body> </html>
OUTPUT
After executing Servlet, you will get the following output in web browser :