February 28, 2021
Hot Topics:

Jetty Continuations: Push Your Java Server Beyond Its Scalability Limits

  • By Manish Malhotra
  • Send Email »
  • More Articles »

Continuation Lifecycle and Business Logic

ServletRequest can be used to communicate the lifecycle to the Servlet component. The demo application uses ServletRequest for this communication: ServletRequest.setAttribute(…), ServletRequest.getAttribute(…) and ServletRequest.getParameter(…).

long timeoutMS = 8000;

Continuation Continuation = ContinuationSupport.getContinuation(request);

String asyncRequestAttr = (String) request.getAttribute("asyncRequest");
String asyncRequestParam = request.getParameter("asyncRequest");

if ((asyncRequestParam !=null && asyncRequestParam.equals("Y"))
(asyncRequestAttr ==null || asyncRequestAttr.equals("N")))
Continuation.addContinuationListener(new MyContinuationListener());
// wait for an event or timeout
Continuation.setAttribute("asyncRequest", "N");
else if (asyncRequestAttr !=null && asyncRequestAttr.equals("Y"))
resp.append("< H2> Happy to see response from AsyncServlet < /H2>");
PrintWriter writer = response.getWriter();
System.out.println("no match for requestParam/Attr there is some
problem..: ");

A pair of methods provided in Continuation are required for maintaining business logic in sync with the Continuation lifecycle: Continuation.getAttribute(…) and Continuation.setAttribute(…). These methods are used to set/update user-defined key/values (different in different stages of Continuation).

In terms of Continuation Listener logic, the following listener code shows how to resume requests in onTimeout methods.

class MyContinuationListener implements ContinuationListener
public void onComplete(Continuation arg0) {
System.out.println("in complete ...: ");
String asyncRequest = (String) arg0.getAttribute("asyncRequest");
System.out.println("request completed: ");
public void onTimeout(Continuation arg0) {
System.out.println("onTimeout resuming ...: ");
String asyncRequest = (String) arg0.getAttribute("asyncRequest");
System.out.println("resumed lets see request onTimeout ");


The onComplete method is implemented because it is required for the class, but it is not very useful in our use case. So, just put in some sysout statements.

After calling Continuation.suspend, the HTTP or TCP/IP connection is not closed. Only the thread that is being assigned to fulfill that request is released to the thread pool in server. This means the lifecycle of the request is extended from the suspend to the resume/complete stages. So with HTTP synchronous connections, the user has to wait until the whole lifecycle is complete (same for HTTP synchronous clients). Therefore, if the user doesn't want to wait for the response and is not dependent on it, he or she can intentionally set the timeout value from the client end lower than the Continuation timeout. That way, the client can send more requests even when the server is trying to process the old request. Jetty also provides an async HTTP Client API that can also be handy in these situations.


Continuation is an innovative extension to a conventional Java-based Web server that is reaching its limits for handling concurrent requests. Using similar principles, Servlet 3.0 has implemented these features. The demo shown in this article builds a custom task-scheduling module within a Web application and uses Jetty Continuation to improve the scalability of the application. Combining this feature with HTTP can useful in many unconventional applications such as distributed processing with low latency and high throughput, where data is not that critical and can be lost during computation. For instance, if the Web server crashes while holding the Continuation request in memory, then the request will be lost if the data is not kept on some reliable platform.


I want to thank my manager Shyam Kumar and Raghavan Subramanian for their guidance and encouragement while I wrote this article.

Code Download

  • TradeTasks.zip

    About the Authors

    Manish Malhotra is a Technology Lead with SETLabs (Software Engineering and Technology Labs), the R&D division of Infosys Technologies Ltd. He works in the JEECoe group. Apart from designing and implementing Java- and Java EE-based applications, he has also worked on software engineering and semantic analysis research.

  • Page 2 of 2

    This article was originally published on October 23, 2010

    Enterprise Development Update

    Don't miss an article. Subscribe to our newsletter below.

    Thanks for your registration, follow us on our social networks to keep up-to-date