AsyncListener and AsyncEvent Example in Servlet 3.0
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.
AsyncListenerDemo.java
package com.concretepage.listener; import java.io.IOException; 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"); @Override public void onComplete(AsyncEvent arg0) throws IOException { logger.info("Inside onComplete method."); } @Override public void onError(AsyncEvent arg0) throws IOException { logger.severe("Inside onError method."); } @Override public void onStartAsync(AsyncEvent arg0) throws IOException { logger.info("Inside onStartAsync method."); } @Override public void onTimeout(AsyncEvent arg0) throws IOException { logger.info("Inside onTimeout method."); } }
TestServlet.java
package com.concretepage.servlet; import java.io.IOException; import java.io.PrintWriter; 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{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); AsyncContext asyncContext = request.startAsync(); //added my listener asyncContext.addListener(new AsyncListenerDemo()); asyncContext.setTimeout(1000); ServletRequest servReq = asyncContext.getRequest(); boolean b = servReq.isAsyncStarted(); out.println("isAsyncStarted : "+b); asyncContext.complete(); out.println("<br/>asynchronous task finished."); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{ doGet(request,response); } }
Output
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
Feb 14, 2014 4:51:17 PM com.concretepage.listener.AsyncListenerDemo onComplete INFO: Inside onComplete method.