Explain Asynchronous processing in Servlet 3.0?
Explain Asynchronous processing in Servlet 3.0?
Sometimes a servlet is unable to complete the processing of a request without waiting for a resource or event before generating a response. For example, a servlet may need to wait for an available JDBC connection, for a response from a remote web service, for a JMS message, or for an application event, before proceeding to generate a response. Waiting within the servlet is an inefficient operation as it is a blocking operation that consumes a thread and other limited resources.
Servlet 3.0 introduces the ability for asynchronous processing of requests so that the thread may return to the container and perform other tasks. When asynchronous processing begins on the request, another thread or callback may either generate the response and call complete or dispatch the request so that it may run in the context of the container using the AsyncContext.dispatch() method as shown below:
REQUEST to /url/A
FORWARD to /url/B
getRequestDispatcher(“/url/B”).forward(request, response);
AsyncContext ac = request.startAsync();
ac.dispatch(); //does a ASYNC dispatch to /url/A