Friday, 27 July 2012

Aynshronous servlet specification

Heap Generation In Java                                                                           Spring Autowiring

Use of Asynchronous Servlet Specification

Use of Asynchronous Servlet Specification

The most basic motivation for asynchronicity in web applications is to handle requests that take longer to complete — possibly running a slow database query, making calls to external REST APIs, or performing other I/O-bound operations. Such requests can quickly exhaust the Servlet container thread pool and affect scalability.
In some cases you may not necessarily have to wait for the processing to complete, e.g. sending an email, kicking off a database job, etc. For such fire-and-forget scenarios you can use servlet's @Async support or place an event  and return immediately, possibly including a confirmation id with the response for later use.
For other cases where the result is required before returning, you'll need a way to decouple request processing from the Servlet container thread in order to improve scalability. To enable this Servlet 3 allows a Servlet to indicate it wants to leave the response open after it has returned so that the request may be completed from a separate thread.
To achieve that the web application can call request.startAsync() and use the returned AsyncContext to continue to write to the response (and eventually finalize it) from a separate thread. From a client's perspective nothing has changed. The request still looks like any other request with standard HTTP request-response semantics.

Below is an example for using asynchronous servlet 3.0 where I am using servlet to accept some parameters from a client and create the POJO'S class object corresponding to it then spool or stored it to a location and then my asynchcronous task is used to insert it into the database(using hibernate version 4).

author:Ishant Agarwal
date:16th july 2012
import java.sql.PreparedStatement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
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;
import org.apache.commons.lang.math.RandomUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

@WebServlet(urlPatterns = "/AsyncDispatcherServlet",asyncSupported=true)
public class AsyncDispatcherServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
 private static final int NUM_ASYNC_TASKS = 100;
 private ExecutorService exececutor;
 private SessionFactory sessionFactory ;
 public void init() throws ServletException {
  exececutor = Executors.newFixedThreadPool(NUM_ASYNC_TASKS) ;

 public void destroy() {
  exececutor.shutdownNow() ;
 public AsyncDispatcherServlet() {
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
  String name = request.getParameter("name") ;
  int age = Integer.parseInt(request.getParameter("age")) ;
  int salary = Integer.parseInt(request.getParameter("salary")) ;
  String designation  = request.getParameter("designation") ;
  boolean isMarried = Boolean.parseBoolean(request.getParameter("ismarried")) ;
  long transactionID = RandomUtils.nextInt();
  final Employee emp  =  new Employee();
  emp.setTransactionId(transactionID) ;
  emp.setAge(age) ;
  emp.setName(name) ;
  emp.setSalary(salary) ;
  emp.setDesignation(designation) ;
  emp.setMarried(isMarried) ;
  ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/spool/" + transactionID));
  oos.writeObject(emp) ;
  oos.flush() ;
  oos.close() ;
  final AsyncContext asynchctx = request.startAsync() ;
  asynchctx.setTimeout(100) ;
  FutureTask task =new FutureTask(new Runnable() {
   public synchronized void run() {
    synchronized (asynchctx) {
    Session session = sessionFactory.openSession() ;
    Transaction tx = session.beginTransaction() ;
     tx.commit() ;
     File f= new File("D:/spool/"+emp.getTransactionId()) ;
     System.out.println("isFileDeleted:" +  f.delete()) ;
     session.close() ;
     }catch(Exception ex){
      ex.printStackTrace() ;
  },null) ;
  asynchctx.addListener(new SimpleAsyncListener()) ;
  exececutor.execute(task) ;
  asynchctx.complete() ;
  System.out.println("Task successfully completed");


import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {
 private static ServiceRegistry serviceRegistry;

 private static final SessionFactory sessionFactory;
 static {
 try {
 Configuration configuration = new Configuration();
 serviceRegistry = new ServiceRegistryBuilder().applySettings(
 sessionFactory = configuration.buildSessionFactory(serviceRegistry);
 } catch (Throwable ex) {
 System.err.println("Failed to create sessionFactory object." + ex);
 throw new ExceptionInInitializerError(ex);
 public static SessionFactory getSessionFactory() {
 return sessionFactory;

package com.lumata.asynch;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

public class Employee implements Serializable{

public String toString() {
 return "Employee [name=" + name + ", age=" + age + ", isMarried="
   + isMarried + ", designation=" + designation + ", salary=" + salary + ", ID=" + id
   + "]";
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + (int) (id ^ (id >>> 32));
  return result;
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  Employee other = (Employee) obj;
  if (id !=
   return false;
  return true;
String name ;
Long id ;

public Long getId() {
 return id;
public void setId(Long id) { = id;

Long transactionId ;

public Long getTransactionId() {
 return transactionId;
public void setTransactionId(Long transactionId) {
 this.transactionId = transactionId;
public String getName() {
 return name;
public void setName(String name) { = name;
public Integer getAge() {
 return age;
public void setAge(int age) {
 this.age = age;
public boolean isMarried() {
 return isMarried;
public void setMarried(boolean isMarried) {
 this.isMarried = isMarried;
public String getDesignation() {
 return designation;
public void setDesignation(String designation) {
 this.designation = designation;
public Integer getSalary() {
 return salary;
public void setSalary(int salary) {
 this.salary = salary;
int age ;
boolean isMarried ;
String designation ;
int salary ;

import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;

public class SimpleAsyncListener implements AsyncListener{

  super() ;

 public void onStartAsync(AsyncEvent ae) {
  System.out.println("AsyncListener: onStartAsync" + ae.getAsyncContext().getRequest().isAsyncStarted());

 public void onComplete(AsyncEvent ae) {
  System.out.println("task completed");
 public void onTimeout(AsyncEvent ae) {
  System.out.println("AsyncListener: onTimeout for request: " +
 public void onError(AsyncEvent ae) {
  Employee emp = (Employee)ae.getAsyncContext().getRequest().getAttribute("emp") ;
  System.out.println("Eror employees Id" + emp) ;


 From a server perspective however, async processing has enabled the handling of requests in a more scalable fashion. The following is the sequence of events one more time:
1)Client sends a request
2)Servlet container invokes a servlet (also allocating a thread)
3)The servlet calls request.startAsync(), saves the AsyncContext, and returns
4)The container thread is released but the response remains open
5)Another thread uses the saved AsyncContext to complete the response
6)Client receives the response

For heap generation and garbage collection click on below given link


  1. Very nice description. Thanks. One doubt i have is can a particular job be executed at a particular scheduled time?

    1. Hi Sangram!!!

      No sangram we cannot use this asynchronous feature to execute a job at a particular schedule time. For this kind of work, you can use quartz scheduler to execute your jobs at a scheduled time.