Saturday, September 17th, 2005

Java Web Server: Jetty 6.0 Continuations for Ajax Architectures

Category: Java, Server

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)
	        // register it with the chatroom to receive async events.
            // Get the continuation object. The request may be suspended here.
            Object event= continuation.getEvent(timeoutMS);
        // send any events that have arrived
        // 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)
         if (_continuation!=null)
           // resume requests suspened in getEvents

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

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.