| By Alex Iskold | Article Rating: |
|
| July 17, 2006 04:45 PM EDT | Reads: |
23,566 |
Pattern: Concurrent Document LoaderProblem: Need to load multiple documents and can't proceed until all of them are loaded
Example: Load configuration files for an AJAX application
Introduction
AJAX stands for Asynchronous JavaScript processing with XML. It is the asynchronous part that gets everyone excited. Fetching the data without reloading the page allows AJAX applications to mimic the desktop-like behavior.
Since AJAX applications are getting quite complex these days, they tend use XML files to store the initial configuration settings and bootstrap information. When the application starts, it needs to load the configuration files in order to setup the application screens.

Loading each configuration time sequentially would take a long time. Clearly, we would like to leverage the power of AJAX and load the files concurrently. To do that we need to create a separate XMLHttpRequest for each configuration file and then dispatch the requests one of another.
We can set the callback on each request, which would be invoked once the configuration file is loaded. But the problem is that all these documents can come at different times, so how do we know when all of them are loaded? To solve this problem, we need to have a loader that keeps track of what has been loaded and invokes a callback once all the documents have been fetched.
Concurrent Document Loader Interface
We start by thinking how we want to interact with this document loader. We want to issue multiple requests and then get a callback when all of them are done.
The callback function will have the array of XMLHttpRequests as its parameter:
So the consumer of this API, would be able to write the following code:

Note how the callback function is declared in place in the example above. This is called closure and looks similar to anonymous functions in Java. Actually it is much more complex than it looks, so if you have not seen this before, you will find complete explanation in this article.
Concurrent Document Loader Implementation
The following implementation has been tested in Firefox 1.5.0.4. We implement the ConcurrentDocumentLoader as a singleton, declaring it like this:

Lets focus on a few things in the code above. The first thing to note is that we keep a count of the number of the outstanding requests using a variable. Every time when a request comes back, we decrement the count. Once the count reaches zero, we know that we are done and we invoke the callback function. This is thread-safe, since there is no one polling on the outstanding requests variable.
The second thing to note is the call to the asyncLoadDocument function. This function is going to do the actual loading using XMLHttpRequest. You can write this function using either your favorite AJAX library like prototype or MochiKit or you can do it by directly manipulating the object as shown in the listing below.

And finally, we list the function that creates XMLHttpRequest in different browsers.

The Concurrent Document Loader pattern is useful in cases when you need to load multiple XML documents concurrently and get notified when all requests are completed.
Download the Source code for this article.
Published July 17, 2006 Reads 23,566
Copyright © 2006 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Alex Iskold
Alex Iskold is the Founder and CEO of adaptiveblue (http://www.adaptiveblue.com), where he is developing browser personalization technology. His previous startup, Information Laboratory, created innovative software analysis and visualization tool called Small Worlds. After Information Laboratory was acquired by IBM, Alex worked as the architect of IBM Rational Software Analysis tools. Before starting adaptiveblue, Alex was the Chief Architect at DataSynapse, where he developed GridServer and FabricServer virtualization platforms. He holds M.S. in Computer Science from New York University, where he taught an award-winning software engineering class for undergraduate students. He can be reached at alex.iskold@gmail.com.
![]() |
Tony 03/21/08 05:26:35 AM EDT | |||
I suspect that this code may not as thread-safe as it claims. We'd have to perform some kind of stress testing to be certain. I recently coded in Javascript a similar concurrent loading problem and avoided use of an integer counter since I suspect that the ++ and -- operations are not as thread-safe. This is because they may require a sequence of fetch, add and save operations that are non-atomic. Instead, I used insert/remove operations on an array as it is more likely that they are implemented using object locking making them atomic and thus thread-safe. |
||||
- Scott Brown to Win Senate Race by a Landslide
- The Next-Generation of Business Intelligence
- BEA Updates WebLogic SOA Portal for Web 2.0 Era
- View Cloud Expo Europe 2009 Keynote on SYS-CON.TV
- Virtualization Expo New York Call for Papers to Expire January 15, 2010
- Cloud Expo New York Call for Papers to Expire January 15, 2010
- Six Enterprise Megatrends to Watch in 2010
- Cloud Expo Europe 2009: Where Are Europe's Cloud Providers?
- Five Gadgets That I Can’t Wait to See in 2010
- Adobe Fiddles with its Web Apps
- Top Ten Benefits of CRM
- Government Social Media & Cloud Computing
- Kindle 2 vs Nook
- Scott Brown to Win Senate Race by a Landslide
- Publishing Synergy: Blog, Twitter and Ulitzer
- The Next-Generation of Business Intelligence
- Ulitzer Names the World's 30 Most Influential Cloud Computing Bloggers
- BEA Updates WebLogic SOA Portal for Web 2.0 Era
- Google Wave
- Wave on Ulitzer: Confessions of a Google Wave Fanboy
- View Cloud Expo Europe 2009 Keynote on SYS-CON.TV
- Cloud Computing Can Revitalize Your Career as Software Developer
- Virtualization Expo New York Call for Papers to Expire January 15, 2010
- Cloud Expo New York Call for Papers to Expire January 15, 2010
- Who Are The All-Time Heroes of i-Technology?
- Where Are RIA Technologies Headed in 2008?
- Success, Arrogance, Rise and Fall
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Personal Branding Checklist
- The Top 150 Players in Cloud Computing
- i-Technology Viewpoint: Attack of the Blogs
- Web 2.0 News and Wrapping Up "Real-World AJAX" Seminar
- Appcelerator Building Out the RIA Open Source Community
- The Top 250 Players in the Cloud Computing Ecosystem
- SOA 2 Point Oh No!
- i-Technology Viewpoint: It's Time to Take the Quotation Marks Off "Web 2.0"
























