Home Servlets Servlet3 AsyncContext Example
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

AsyncContext Example

Advertisement
In this section, you will learn about Servlet 3.0 AsyncContext using a complete example.

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 :  

 

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 16, 2012

Related Tutorials

Ask Questions?    Discuss: AsyncContext Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
tojasoft
August 5, 2013
AsyncContext Example works only on glasfish 3.1.2

why??? you writing this example is for tomcat 7! I got the follow message from tomcat7: java.lang.IllegalStateException: Not supported. org.apache.catalina.connector.Request.startAsync(Request.java:1673) org.apache.catalina.connector.Request.startAsync(Request.java:1666) org.apache.catalina.connector.RequestFacade.startAsync(RequestFacade.java:1023) javax.servlet.ServletRequestWrapper.startAsync(ServletRequestWrapper.java:364) roseindia.AsyncContextDispatchMethod.doGet(AsyncContextDispatchMethod.java:29) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:728) org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) note The full stack trace of the root cause is available in the Apache Tomcat/7.0.34 logs.ot the follow message:
DMCA.com