5.2 An example
In order to complete this section I shall present a simple example. This is loosely based on one described in , currently one of the very few books written on JavaSpaces technology.
An object that can be stored in a space has to implement an interface . The objects that form part of the example will just be strings which will be concatenated by characters fed to them by client objects. The code for an entry is shown below:
You will probably have noticed that the instance variable in the code is public; this goes against a main principle of object-oriented design. However, the designers of the JavaSpaces technology have implemented the technology in such a way that all instance variables have to be declared public. Another restriction is that all instance variables must be objects not scalars such as . If you want to use scalars you need to use their corresponding wrapper class.
is an interface with no methods; it acts as a tag which informs the JavaSpaces run-time system that the class which implements it will generate objects that can be stored within a space. In this respect it is similar to interfaces such as .
This, then, is the code for the objects contained in a space. The code for placing them in a space and reading their contents is shown below; it places one object in a single space:
What this code does is first to create a new space using the code
and then writes a object to the space using the code
Note the exception handling. An exception will be thrown if a problem occurs such as a faulty connection between the computer implementingthe spaces.
The method has three arguments, the first is the object that is to be written, the second is a transaction associated with the object and the third argument specifies how long the object is to be resident in the space; in the example the constant specifies that it will effectively be resident forever.
The remaining part of the code will loop forever retrieving the object from the space displaying its current value. The core part of this code is the statement
This uses the method which reads an object from a space. This method has three arguments. The first argument is a object which acts as a template for retrieving objects from a space. What does is to find an object which has its instance variables equal to the instance variables found in the first argument. Since these variables are set to for the method will retrieve any object – since there has been only one object deposited in the space it will be this object that is retrieved. The second argument is the transaction associated with the used – since there is no transaction this is . The final argument to the method specifies a time-out period; if the has not retrieved the object by the time that the period has been completed, the method will return . Normally an object might be timed out if other client objects are currently accessing the object for longish periods. In the example I have specified a very long time-out period so that the operation will always complete even if other objects were accessing the object for some time.
The code above is for a simple server which places an object in a space and continually retrieves its value and displays it. The remaining client code will modify objects in a space so that the server code above will display different results. The code is shown below:
The code reflects the structure of the code of the class . The main work is carried out in the endless loop which extracts the object stored in the space, concatenates it with a single character and then writes it back to the space. When the server and client is executed what will be displayed by the server will be lines such as
where the number of asterisks following the string will depend on factors such as the latency of the network in which the spaces are implemented and the loading of the computer in which the code is placed and its power.
There are three components to the simple distributed system that I have presented: the class which implements the objects stored in a space, the class which represents a simple server that deposits an object in a space and continually monitors the state of the object and a client class which retrieves and updates the state of an object in the space.
The tuple model is a little more complicated than the example above has, perhaps, suggested. However, it is not very much more complicated and compared with, say, message passing techniques it has a breathtaking simplicity.