Blog Image

Asynchronous Servlet  

What is asynchronous servlet? What is the need of asynchronous support in web application?  is explained as part of this blog. The basic example of asynchronous servlet is given as part of the blog and attachment.

In web application the servlet container deletegate request to one thread from the thread pool. If the multiple request hitting to web application at a time, each thread in thread pool will participate in processing on request. In such condition there will not be threads available for other requests. These requests has to wait until the threads complete the execution of the previous threads. This is called thread starvation or thread blocking.

In Java EE, thread starvation issue is avoided by providing asynchronous processing support to servlet and filter. If a servlet reaches a blocking operation during processing a request, it will assign the request processing to an asynchronous execution context which will take care of further processing and response generation. And the servlet thread will become free to process the other request so that thread starvation or blocking issues can be avoided.

To use asynchronous processing in our application we have to set the parameter asyncSupported to true on the @WebServlet annotation as shown below:


@WebServlet(urlPatterns={"/servlet"}, asyncSupported=true)
public class AsyncServlet extends HttpServlet { ... }


AsynchronousContext is the class provide to get asynchronous processing in web application whose instance can be added as follows:

AsyncContext acontext = req.startAsync();



The following is the basic example for getting the request in asynchronous processing support:


@WebServlet(asyncSupported = true, value = "/AsyncContextExample", loadOnStartup = 1)
public class AsynchronousServlet extends HttpServlet {
    private static final long serialVersionUID = -4967506680700041008L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       
        Date date = new Date();
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        request.setAttribute("reqDt", date);
        out.println("Request Dt :" + request.getAttribute("reqDt"));
        AsyncContext asyncCtx = request.startAsync();
        ServletRequest req = asyncCtx.getRequest();
        boolean bolean = req.isAsyncStarted();

        /* This will return true*/
        out.println("<br>AsyncStarted : " + bolean);

        if (bolean) {
            asyncCtx.dispatch("/test.jsp");
        }

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}


The output for the above example is as follow:

Request Dt :Tue Dec 12 20:41:27 IST 2017
AsyncStarted : true


In the above program, requset.startAsync() used to get the instance of AsyncContext. If the request is supports asynchronous process req.isAsyncStarted() return 'true'. dispatch() in AsyncContext is used to dispatch the request to the corresponding jsp page.

You can download the above program from below attachment.

About author

User Image
AdityaT

I am a software developer. I am working working on jdk , jee technologies. I am working on Spring Framework as well. I am passionate about learning java technologies like Web Services and Restful Services.

3

-Comments

Be the first person to write a comment for this Blog
  • author image
    by:
      ali032
      13-12-2017 06:37:15 PM

    Thanks a lot for the sharing valuable knowledge...

  • author image
    by:
      ali032
      13-12-2017 06:32:37 PM

    thanks a lot for sharing this valuable knowledge....

  • author image
    by:
      Manjunatha1
      13-12-2017 10:09:33 AM

    Thank you sharing a your knowledge we are also improving a my self to technically strong i happy to read this content ..

Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.