Saturday, September 17th, 2005

Java Web Server: Jetty 6.0 Continuations for Ajax Architectures

Category: Java, Server

<p>Jetty is an open source web container for Java. The team just announced a new version that supports Continuations, which allow performance improvements for certain Ajaxian applications:

The 6.0.0alpha3 release of Jetty is now available and provides a 2.4 servlet server in 400k jar, with only 140k of dependencies (2.6M more if you want JSP!!!). But as well as being small, fast, clean and sexy, Jetty 6 supports a new feature called Continuations that will allow scalable AJAX applications to be built, with threadless waiting for asynchronous events.

Ajax polling problem

But there is a new problem. The advent of AJAX as a web application model is significantly changing the traffic profile seen on the server side. Because AJAX servers cannot deliver asynchronous events to the client, the AJAX client must poll for events on the server. To avoid a busy polling loop, AJAX servers will often hold onto a poll request until either there is an event or a timeout occurs.

Thus an idle AJAX application will have an outstanding request waiting on the server which can be used to send a response to the client the instant an asynchronous event occurs. This is a great technique, but it breaks the thread-per-request model, because now every client will have a request outstanding in the server. Thus the server again needs to have one or more threads for every client and again there are problems scaling to thousands of simultaneous users.

Jetty 6 Continuations

The solution is Continuations, a new feature introduced in Jetty 6. A java Filter or Servlet that is handling an AJAX request, may now request a Continuation object that can be used to effectively suspend the request and free the current thread. The request is resumed after a timeout or immediately if the resume method is called on the Continuation object. In the Jetty 6 chat room demo, the following code handles the AJAX poll for events:

private void doGetEvents(HttpServletRequest request, AjaxResponse response) {
        member = (Member)chatroom.get(request.getSession(true).getId());

        // Get an existing Continuation or create a new one if there are no events.
        boolean create=!member.hasEvents();
        Continuation continuation=ContinuationSupport.getContinuation(request,create);
        
        if (continuation!=null)
        {
            if(continuation.isNew())
	        // register it with the chatroom to receive async events.
                member.setContinuation(continuation);
        
            // Get the continuation object. The request may be suspended here.
            Object event= continuation.getEvent(timeoutMS);
        }
        
        // send any events that have arrived
        member.sendEvents(response);
        
        // Signal for a new poll
        response.objectResponse("poll", "");
}

When another user says something in the chat room, the event is delivered to each member by another thread calling the method:

  class Member
  {
       public synchronized void addEvent(Event event)
       {
         _events.add(event);
         if (_continuation!=null)
           // resume requests suspened in getEvents
           _continuation.resume(event); 
       }
       ...
  }

Great to see server vendors helping us out on issues that come about from the new paradigm!

Related Content:

  • ClearNova integrates Java and Ajax
    Making sure that Java-built Web services don't end at the server level, ClearNova has built a development environment that plugs Java into an Ajax...
  • Web services, AJAX loom for developers
    The rise of Web services marks a shift in focus to AJAX and client-side development. Microsoft and others will make sure developers don't have to...
  • Web services, AJAX loom for developers
    The rise of Web services marks a shift in focus to AJAX and client-side development. Microsoft and others will make sure developers don't have to...
  • Web services, AJAX loom for developers
    The rise of Web services marks a shift in focus to AJAX and client-side development. Microsoft and others will make sure developers don't have to...
  • Ajax and Java Web services
    This chapter from "SOA Using Java Web Services" examines how to build an Ajax front-end to an online shopping application. Through detailed code...

Posted by Dion Almaer at 1:24 am
Comment here

+++--
3.2 rating from 19 votes

Comments Here »

Comments feed

Leave a comment

You must be logged in to post a comment.