September 17, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Jetty Continuations: Push Your Java Server Beyond Its Scalability Limits

  • October 22, 2010
  • 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.setTimeout(timeoutMS);
Continuation.setAttribute("asyncRequest", "N");
Continuation.suspend();
return;
}
else if (asyncRequestAttr !=null && asyncRequestAttr.equals("Y"))
{
resp.append("< H2> Happy to see response from AsyncServlet < /H2>");
PrintWriter writer = response.getWriter();
writer.write(resp.toString());
writer.flush();
writer.close();
return;
}
else
{
System.out.println("no match for requestParam/Attr there is some
problem..: ");
return;
}

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
{
@Override
public void onComplete(Continuation arg0) {
System.out.println("in complete ...: ");
String asyncRequest = (String) arg0.getAttribute("asyncRequest");
System.out.println(asyncRequest);
arg0.setAttribute("asyncRequest","N");
//ASync
System.out.println("request completed: ");
}
@Override
public void onTimeout(Continuation arg0) {
System.out.println("onTimeout resuming ...: ");
String asyncRequest = (String) arg0.getAttribute("asyncRequest");
System.out.println(asyncRequest);
arg0.setAttribute("asyncRequest","Y");
//ASync
arg0.resume();
System.out.println("resumed lets see request onTimeout ");
}




dads
}

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.

Conclusion

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.

Acknowledgments

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.


  • Tags: Jetty, Servlets



    Page 2 of 2



    Comment and Contribute

     


    (Maximum characters: 1200). You have characters left.

     

     


    Sitemap | Contact Us

    Rocket Fuel