Monday 8 September 2014

AsyncListener

This listener notified when an asynchronous operation initiated on a ServletRequest to which the listener had been added has completed, timed out, or resulted in an error.

Method Description
onComplete(AsyncEvent event) Notified when Asynchronous operation completed Execution.
onError(AsyncEvent event) Notified when an asynchronous operation has failed to complete.
onStartAsync(AsyncEvent event) Notifies this AsyncListener that a new asynchronous cycle is being initiated.
onTimeout(AsyncEvent event) Notified when an asynchronous operation has timed out.

import java.io.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.servlet.*;

@WebServlet(urlPatterns = {"/AsynchListenerEx"}, asyncSupported=true)
public class AsynchListenerEx extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{
            final AsyncContext acontext = req.startAsync();
            
            String threadName = Thread.currentThread().getName();
            long startTime = System.currentTimeMillis();
            System.out.println("Thread " + threadName + " started at " + startTime);

            acontext.addListener(new AsyncListener(){

                @Override
                public void onComplete(AsyncEvent event) throws IOException {
                    System.out.println("Asynchronous operation completed");
                }

                @Override
                public void onTimeout(AsyncEvent event) throws IOException {
                    System.out.println("Asynchronous operation timed out");
                }

                @Override
                public void onError(AsyncEvent event) throws IOException {
                    System.out.println("Asynchronous operation failed to complete");
                }

                @Override
                public void onStartAsync(AsyncEvent event) throws IOException {
                    System.out.println("Asynchronous operation initiated");
                }
                
            });
            
            acontext.start(new Runnable() {

                @Override
                public void run() {
                    String time = req.getParameter("time");
                    int wait = Integer.parseInt(time);
                    
                    try{
                        Thread.sleep(wait);
                    }
                    catch(InterruptedException e){
                        
                    }
                    HttpServletResponse response = (HttpServletResponse)acontext.getResponse();
                    try(PrintWriter out = response.getWriter()) {
                        out.println("Done");
                    }
                    catch(Exception e){
                        
                    }
                    acontext.complete();
                }
            });
            
            long endTime = System.currentTimeMillis();
            System.out.println("Thread " + threadName + " finished at " + endTime);
            System.out.println("Total time taken is " +(endTime-startTime) + " Milli Seconds");
    }
}

Run the servlet, below information can be seen in server console file.

Info:   Thread http-listener-1(5) started at 1407069470162
Info:   Thread http-listener-1(5) finished at 1407069470163
Info:   Total time taken is 1 Milli Seconds
Info:   Asynchronous operation completed




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment