AsyncListener and AsyncEvent Example in Servlet 3.0

By Arvind Rai, February 14, 2014
javax.servlet.AsyncListener and javax.servlet.AsyncEvent has been introduced in Servlet 3.0. AsyncListener is associated with AsyncContext which is used for asynchronous operation in a servlet. AsyncListener is associated with AsyncContext . AsyncListener listens on different stages of AsyncContext like onComplete, onError, onStartAsync and onTimeout and AsyncEvent is passed as an argument. In our demo of AsyncListener, we will create a listener and it will be used in a servlet associated with asynchronous operation.
For the demo, we have used below software and tool.
1. JDK 7
2. Tomcat 7
3. Eclipse
Find the classes which is being used in the Demo.
package com.concretepage.listener;
import java.util.logging.Logger;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
public class AsyncListenerDemo implements AsyncListener {
	private static Logger logger = Logger.getLogger("conlog");
	public void onComplete(AsyncEvent arg0) throws IOException {"Inside onComplete method.");
	public void onError(AsyncEvent arg0) throws IOException {
		logger.severe("Inside onError method.");
	public void onStartAsync(AsyncEvent arg0) throws IOException {"Inside onStartAsync method.");
	public void onTimeout(AsyncEvent arg0) throws IOException {"Inside onTimeout method.");
There are different methods of AsyncListener which needs to be implemented in implementing class. Now in the below servlet, we have started an asynchronous operation using AsyncContext.
package com.concretepage.servlet;
import javax.servlet.AsyncContext;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.concretepage.listener.AsyncListenerDemo;

@WebServlet(asyncSupported = true, value = "/TestServlet", loadOnStartup = 1)
public class TestServlet extends HttpServlet   {
    private static final long serialVersionUID = 1L;
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
      PrintWriter out = response.getWriter();
      AsyncContext asyncContext = request.startAsync();
      //added my listener
      asyncContext.addListener(new AsyncListenerDemo());
      ServletRequest servReq = asyncContext.getRequest();
      boolean b = servReq.isAsyncStarted();
      out.println("isAsyncStarted : "+b);
      out.println("<br/>asynchronous task finished.");	
   public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{


To run the application, download the source code and run it using the URL http://localhost:8080/javaee-1/TestServlet We will observe two types of output on browser and on console. Browser output will be
AsyncListener Example in Servlet 3.0
And console output will be
Feb 14, 2014 4:51:17 PM com.concretepage.listener.AsyncListenerDemo onComplete
INFO: Inside onComplete method.

Download Source Code


©2024 | Privacy Policy | Contact Us