servlet

Java Servlet Sync Context Example

Async Servlet was introduced in Servlet 3.0. It is a great way to deal with the thread starvation problem with the long-running threads. In this tutorial, we will understand what Async Servlet is.
 
 
 
 
 
 
 
 
 
 

1. Introduction

Let’s say we have a Servlet that takes a lot of time to process, something like below.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    long startTime = System.currentTimeMillis();
    System.out.println("MyServlet Start :: Name?= " + Thread.currentThread().getName() + " :: ID?= " + Thread.currentThread().getId());

    String time = request.getParameter("time");
    int secs = Integer.valueOf(time);
    if (secs > 10000) {
        secs = 10000;
    }
    longProcessing(secs);
    PrintWriter out = response.getWriter();
    long endTime = System.currentTimeMillis();
    out.write("Processing done for " + secs + " milliseconds !!");
    System.out.println("MyServlet Start :: Name?= " + Thread.currentThread().getName() + " :: ID?= " + Thread.currentThread().getId() + " :: Time Taken?= " + (endTime - startTime) + " ms.");
}

private void longProcessing(int secs) {        
try {
        Thread.sleep(secs);
    } catch (InterruptedException exObj) {
        exObj.printStackTrace();
    }
}

If we hit the above servlet through the browser with URL as http://localhost:8085/JavaServletASyncContextEx/MyServlet?time=8000, developers will get the response as Processing done for 8000 milliseconds !! after 8 seconds. Now if developers will look into the server logs, they will get the following logs:

MyServlet Start :: Name?= http-bio-8080-exec-34 :: ID?= 103
MyServlet Start :: Name?= http-bio-8080-exec-34 :: ID?= 103 :: Time Taken?= 8002 ms.

So the Servlet Thread was running for ~8+ seconds, although most of the processing has nothing to do with the servlet request or response. This can lead to Thread Starvation as the servlet thread is blocked until all the processing is done. If a server gets a lot of requests to process, it will hit the maximum servlet thread limit and further requests will get the Connection Refused errors.

1.1 Need for Asynchronous Servlet Implementation

Often during handling the Servlet request, the application thread is waiting for some external resource due to which it becomes idle for some time. Due to this, developers are engaging the thread and therefore a lot of memory is occupied by you without doing any function. Consider a situation where the application is providing the downloading of files with limited output. In this case, the threads are idle most of the time since they are waiting to send next bundle of data. Prior to Servlet 3.0, developers couldn’t attend or process more than the HTTP thread limit.

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

Method startAsync() doesn’t create any thread. Means it will not create a new thread for every Async request. It just tells the Servlet container that does not close this request until application’s code tells you to do so.

Now, open up the Eclipse Ide and let’s see how to implement the Servlet 3.0 Async Context!

2. Java Servlet Async Context Example

Here is a step-by-step guide for implementing the Servlet Sync Context in Java.

2.1 Tools Used

We are using Eclipse Kepler SR2, JDK 8 and Maven. Having said that, we have tested the code against JDK 1.7 and it works well.

2.2 Project Structure

Firstly, let’s review the final project structure, in case you are confused about where you should create the corresponding files or folder later!

Fig. 1: Application Project Structure
Fig. 1: Application Project Structure

2.3 Project Creation

This section will demonstrate on how to create a Java-based Maven project with Eclipse. In Eclipse Ide, go to File -> New -> Maven Project.

Fig. 2: Create Maven Project
Fig. 2: Create Maven Project

In the New Maven Project window, it will ask you to select project location. By default, ‘Use default workspace location’ will be selected. Just click on next button to proceed.

Fig. 3: Project Details
Fig. 3: Project Details

Select the ‘Maven Web App’ Archetype from the list of options and click next.

Fig. 4: Archetype Selection
Fig. 4: Archetype Selection

It will ask you to ‘Enter the group and the artifact id for the project’. We will input the details as shown in the below image. The version number will be by default: 0.0.1-SNAPSHOT.

Fig. 5: Archetype Parameters
Fig. 5: Archetype Parameters

Click on Finish and the creation of a maven project is completed. If you observe, it has downloaded the maven dependencies and a pom.xml file will be created. It will have the following code:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>JavaServletASyncContextEx</groupId>
	<artifactId>JavaServletASyncContextEx</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
</project>

We can start adding the dependencies that developers want like Servlets, Junit etc. Let’s start building the application!

3. Application Building

Below are the steps involved in developing this application.

3.1 Maven Dependencies

Here, we specify the dependencies for the Servlet API. The rest dependencies will be automatically resolved by the Maven framework and the updated file will have the following code:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>JavaServletASyncContextEx</groupId>
	<artifactId>JavaServletASyncContextEx</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>JavaServletASyncContextEx Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>${project.artifactId}</finalName>
	</build>
</project>

3.2 Java Class Creation

Let’s create the required Java files. Right-click on src/main/java folder, New -> Package.

Fig. 6: Java Package Creation
Fig. 6: Java Package Creation

A new pop window will open where we will enter the package name as: com.jcg.servlet.async.

Fig. 7: Java Package Name (com.jcg.servlet.async)
Fig. 7: Java Package Name (com.jcg.servlet.async)

Once the package is created in the application, we will need to create the controller classes. Right-click on the newly created package: New -> Class.

Fig. 8: Java Class Creation
Fig. 8: Java Class Creation

A new pop window will open and enter the file name as: AsyncContextDispatch. The Servlet Controller class will be created inside the package: com.jcg.servlet.async.

Fig. 9: Java Class (AsyncContextDispatch.java)
Fig. 9: Java Class (AsyncContextDispatch.java)

3.2.1 Implementation of Controller Class

In this example, developers will learn how to initialize the AsyncContext using the ServletRequest object and dispatch the request and response objects of the AsyncContext to a given URL. Let’s see the simple code snippet that follows this implementation.

AsyncContextDispatch.java

package com.jcg.servlet.async;

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 = "/AsyncContextDispatch", asyncSupported = true)
public class AsyncContextDispatch extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		Date dateObj = new Date();

		resp.setContentType("text/html");
		PrintWriter out = resp.getWriter();
		out.print("<h2>AsyncContext Example </h2>");

		req.setAttribute("receivedAt", dateObj);
		out.println("Request Time?= " + req.getAttribute("receivedAt"));

		AsyncContext asyncCtx = req.startAsync();
		ServletRequest servReq = asyncCtx.getRequest();

		boolean isAsyncStarted = servReq.isAsyncStarted();
		// This Will Return True
		out.println("<br>AsyncStarted?= " + isAsyncStarted);
		if (isAsyncStarted) {
			asyncCtx.dispatch("/asyncOutput.jsp");
		}

		boolean isAsyncSupported = req.isAsyncSupported();
		// This Will Return True
		out.println("<br>AsyncSupported?= " + isAsyncSupported);
	}
}

3.3 Creating JSP Views

Servlet 3.0 supports many types of views for the different presentation technologies. These include: JSP, HTML, XML etc. So let us write a simple view in JavaServletASyncContextEx/src/main/webapp/. Add the following code to it:

asyncOutput.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
	    <title>Asynchronous Servlet 3.0 Example</title>
	    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">	    
	</head>
	<body>
	    <h3>Given Above Is The Servlet 3.0 AsyncContext Interface Dispatch() Method Example</h3>
	</body>
</html>

4. Run the Application

As we are ready for all the changes, let us compile the project and deploy the application on the Tomcat7 server. To deploy the application on Tomat7, right-click on the project and navigate to Run as -> Run on Server.

Fig. 10: How to Deploy Application on Tomcat
Fig. 10: How to Deploy Application on Tomcat

Tomcat will deploy the application in its web-apps folder and shall start its execution to deploy the project so that we can go ahead and test it in the browser.

5. Project Demo

Open your favorite browser and hit the following URL. The output page will be displayed.

http:// localhost:8085/JavaServletASyncContextEx/AsyncContextDispatch

Server name (localhost) and port (8085) may vary as per your tomcat configuration. Developers can debug the example and see what happens after every step. Enjoy!

Fig. 11: Application Output
Fig. 11: Application Output

That’s all for this post. Happy Learning!!

6. Conclusion

In this section, developers learned how to implement the AsyncContext in a Servlet. Developers can download the sample application as an Eclipse project in the Downloads section. I hope this article served you with whatever developers were looking for.

7. Download the Eclipse Project

This was an example of AsyncContext in a Servlet.

Download
You can download the full source code of this example here: JavaServletASyncContextEx

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button