AsyncContext complete method example

In this section, you will learn about complete method of AsyncContext Interface using an example.

AsyncContext complete method example

AsyncContext complete method example

In this section, you will learn about complete method of AsyncContext Interface using an example.

Need of AsyncContext

Often during handling request, your application thread is waiting for some external resource due to which it become idle for some time. Due to this, you are engaging the thread and therefore a lot of memory is occupied by you without doing any function.

Consider a situation where your application is providing the downloading of files with limited output. In this case, your threads are idle most of the time since they are awaiting to send next bundle of data. Prior to Servlet 3.0 , you couldn't attend / process more than your HTTP thread limit.

With Servlet 3.0, you can attend/ process thousands of connections concurrently which is much more than thread limit. It means you can connect to thousands of client with few HTTP threads.

complete() method

This method is used when the asynchronous operation was completed. This method is called on the AsyncContext object. It also close the response that was used to initialized this AsyncContext.

Tools & technology used :

The tools and technologies used to run the below example is listed below :

  • Eclipse Helios 3.6.1

  • Tomcat 7

  • jdk1.6.0_18

EXAMPLE

In the below example, you will learn how to initialize a AsyncContext using ServletRequest object and dispatches the request and response objects of the AsyncContext to a given URL. And also, how to completes the asynchronous operation using complete() method.

AsyncContextCompleteMethod.java

package roseindia;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = "/AsyncContextCompleteMethod", asyncSupported = true)
public class AsyncContextCompleteMethod extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

Date date = new Date();
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.print("<h2>AsyncContext Complete Method Example </h2>");
request.setAttribute("receivedAt", date);
out.println("Request Time :" + request.getAttribute("receivedAt"));
AsyncContext asyncCtx = request.startAsync();
ServletRequest req = asyncCtx.getRequest();
boolean bol = req.isAsyncStarted();

boolean bol2 = req.isAsyncSupported();

// This will return true
out.println("<br>AsyncSupported : " + bol2);

// This will return true
out.println("<br>AsyncStarted : " + bol);

// complete Method
asyncCtx.complete();

boolean bol1 = req.isAsyncStarted();

// This will return false
out.println("<br>AsyncStarted : " + bol1);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

OUTPUT

When  you execute the above code, you will get the following page :

URL : http://localhost:8080/Servlet3Examples/AsyncContextCompleteMethod

Download Source Code