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:

  • Threadless Ajax with Jetty 6.0
    Synchronous servlet APIs threaten to bog down the scalability of of Ajax with too many user threads, but Jetty 6.0 has found a way to handle those...
  • Ajax features added to MyEclipse
    Genuitec LLC. announced the release of the commercial MyEclipse 6.0, offering advanced features including Ajax development and testing tools and...
  • Ajax Tutorial
    Ajax, short for Asynchronous Java and XML, has allowed developers to create interactive Web pages with rich interfaces. Rich Internet applications...
  • Ajax Learning Guide
    Are you a Web developer? The time has come to rethink your entire approach to developing Web applications. Find out about the Ajax approach...
  • Ajax development: The what, how and when, continued -- Five tips for getting started
    Most experts agree that the first steps forward in Ajax development should be small ones. Here are five tips for introducing Ajax to your Web...

Posted by Dion Almaer at 1:24 am
Comment here

+++--
3.3 rating from 14 votes

Comments Here »

Comments feed

Leave a comment

You must be logged in to post a comment.