Evaluate Weigh the pros and cons of technologies, products and projects you are considering.

How Web services can use JavaSpaces

William Brogden applies JavaSpaces to a Web services implementation, breaking down the basic operations and sequence of events involved in using this style of grid computing.

In my previous article I laid out some differences between the various technologies that get lumped under the term "grid computing" and how Web services and grid computing can work together. Now I would like to describe in more detail how "space" style grid computing actually works, using open source JavaSpaces as an example.

To recap, "space" style distributed computing is characterized by loosely coupled systems, which are coordinated through a single service acting as a shared associative memory for data containing objects. In a Web service context, the service would take a request that requires extensive computation or other resources and create an object representing the job. This object would be written into a space where worker processes have registered as having the capability of doing specific kinds of jobs. On completion of the job, the Web service would get back an object containing the results and format a reply to the requesting system.

The Example Problem

I am going to use an example that is a simple version of a general information lookup problem: given a partial description, locate all the entities that might fit that description. In this case we have a word which may or may not be spelled correctly. This may come from a court reporter notes trying to represent what a witness said phonetically. We have the attempt at spelling the word plus some context as to the way the word was used. We need to find a list of words that might have been intended. This will be done by creating an object containing the information we have and letting lookup workers try to match it.

What a JavaSpace Holds

In order to place objects in a JavaSpace, you must have a class which implements the Entry interface (which extends the Serializable interface). Furthermore, each field must have a public object reference - no primitives. Here is the Java class I used - "Metaphone" is the name of the phonetic encoding algorithm. Entries are used both to represent jobs and as templates by which workers notify a space that they want entries having certain characteristics.

public class MetaphoneLookupEntry implements
net.jini.core.entry.Entry {

public String word ; // the starting point word
public String domain ; // ie names, places, companies, etc
public Boolean filled = Boolean.FALSE ;
public String code ; // metaphone code
public String worker ; // so we can track who does what public String[] matching ;
public MetaphoneLookupEntry() {
public MetaphoneLookupEntry(String wd, String type ){ word = wd ; domain = type ;

A worker will calculate the Metaphone code for a word and use that to look up possible matches in a list for the specified domain. Matches go into the "matching" String array and the "filled" field will be set to TRUE. If there is no match the String array will be empty.

Basic JavaSpace Operations

JavaSpaces support four basic operations that turn out to provide tremendous capabilities for organizing distributed computing. Note that a space does not modify an entry object, it only holds objects and communicates with other programs. The basic operations are "write," "take," "read" and "notify." For the lookup service we only need the simplest versions.

The "write" operation puts an object into a space and specifies the length of time the entry will be valid, known as the lease duration. For a Web service, the lease should be short so that a failure becomes apparent quickly. For example, a service needing a lookup might write a MetaphoneLookupEntry with name="pittsburgh", type="place" and the filled field equal FALSE into a space with a lease time of 10 seconds.

The "take" operation uses an Entry as a template where data fields that are null act as wild cards and data fields that have values must be matched. A worker that knows how to look up "place" names would create a MetaphoneLookupEntry object where domain="place," filled=FALSE and all other fields remain null so anything will match and use that in a "take" operation. "Take" operations also specify an amount of time to wait. The "take" operation will either return an Entry with a job or null indicating that no job is available.

The "read" operation is similar to "take," but only gets a copy, the original Entry remains in the space. The "notify" operation sets up a call-back link by which the space server will notify a worker when a matching Entry has been written into the space.

An Example Sequence of Events

Here is how a Web service could make use of a JavaSpaces service. We assume that one or more worker processes has done a "take" operation with a template specifying type="place" and filled=FALSE. Note that the worker process thread will be stalled until the take returns or the lease times out. Any number of worker process threads may be in this state.

  * The Web service does a write operation with an Entry having type="place," name="pittsburgh" and filled=FALSE. Immediately it does a "take" with the same values except having filled=TRUE, this will retrieve the Entry when a worker writes it back to the space.

  * One of the worker "take" operations succeeds, resulting in an Entry specifying a needed lookup. The worker fills in the matching String array, sets filled=TRUE and writes the Entry back to the space.

  * The Web service "take" with filled=TRUE succeeds, recovering the Entry with lookup results filled in with possible matches. In the word list I used, "Pitsburg", "Pittsboro", "Pittsburg" and "Pottsboro" were found as phonetic matches.

Advantages and Disadvantages

Although JavaSpaces servers are not trivial to set up, they are much easier than any other type of grid computing server. Furthermore, the simplicity of the interface makes the learning curve easier. The greatest advantage of the JavaSpaces approach is the ease with which additional workers can be added to the grid.

It should be clear from the example that there is a lot of extra communication traffic in a JavaSpaces solution so the only reason to use JavaSpaces or any other form of grid computing in support of a Web service is a requirement for computing power or special resources that are not feasible to supply on the server directly.


Recent tutorial on JavaSpaces

Jini starter kit, including a JavaSpaces implementation

GigaSpaces commercial and community edition JavaSpaces implementations

Apache Jakarta implementation of the Metaphone Algorithm

Moby Words public domain source

Dig Deeper on Topics Archive

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.