Java · Servlet3.0

Asynchronous Servlet in 3.0

Async servlet is a new feature added in servlet 3.0 specification. This servlet works in a non-blocking mode. Each request is processed in a new thread which is independent of request thread. The request is temporarily suspended by application till the business logic is executed. Once the application is ready with result, the request will be resumed and the response will be pushed to browser.

The request is temporarily suspended and later resumed or re-dispatched through filters for further processing. This is completely dependent on persistent connection between a browser and server. And all the event which are generated while executing the request are relayed on existing persistent connection.

Required Environment:

Jetty9.x version server Or Tomcat Latest Version.

And example of AyncServlet

package com.servlets;

import java.io.IOException;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AsyncServletDis
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/asyncservletdis" })
public class AsyncServletDis extends HttpServlet {
 private static final long serialVersionUID = 1L;
 int instanceVariable =1;

 /**
 * @see HttpServlet#HttpServlet()
 */
 public AsyncServletDis() {
 super();
 // TODO Auto-generated constructor stub
 }

 /**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 /*
 * This is an important step to start asynchronous nature of servlet
 * Get the AsyncContext
 */

 AsyncContext aCtx = request.startAsync();
 // add a listener to this context.
 aCtx.addListener(new AsyncListener());
 //aCtx.setTimeout(5000);
 aCtx.start(new Runnable() {

 @Override
 public void run() {
 // TODO Auto-generated method stub

 String sleep = request.getParameter("sleep");
 if(sleep!=null){
 try {
 Thread.sleep(10000);
 } catch (InterruptedException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 }

 try {
 aCtx.getResponse().getWriter().write("Request Processed");
 System.out.println("Print instance variable#####"+instanceVariable);
 instanceVariable+=1;
 } catch (IOException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }
 /*
 * Once completed call the complete method to close asynch process.
 * Either call complete or dispatch to end thread.
 */

 aCtx.complete();
 //aCtx.dispatch("/result.jsp");

 }
 });
 }

 /**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 // TODO Auto-generated method stub
 }

}

And Listener

package com.servlets;

import javax.servlet.AsyncEvent;
import javax.servlet.annotation.WebListener;

import com.sun.media.jfxmedia.logging.Logger;

/**
 * Application Lifecycle Listener implementation class AsyncListener
 *
 */
@WebListener
public class AsyncListener implements javax.servlet.AsyncListener {

 /**
 * Default constructor. 
 */
 public AsyncListener() {
 // TODO Auto-generated constructor stub
 }

 /**
 * @see AsyncListener#onComplete(AsyncEvent)
 */
 public void onComplete(AsyncEvent arg0) throws java.io.IOException { 
 // TODO Auto-generated method stub
 System.out.println("Asyn proces completed..");
 }

 /**
 * @see AsyncListener#onError(AsyncEvent)
 */
 public void onError(AsyncEvent arg0) throws java.io.IOException { 
 // TODO Auto-generated method stub
 }

 /**
 * @see AsyncListener#onStartAsync(AsyncEvent)
 */
 public void onStartAsync(AsyncEvent arg0) throws java.io.IOException { 
 // TODO Auto-generated method stub
 System.out.println("Async process started..."+arg0.getAsyncContext());

 }

 /**
 * @see AsyncListener#onTimeout(AsyncEvent)
 */
 public void onTimeout(AsyncEvent arg0) throws java.io.IOException { 
 // TODO Auto-generated method stub
 }

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s