|
The Hello World programme described previously consisted of a single process; this hardly warrents the use of a distributed approach!.
Table 1 shows an extended version of the Message class described previously, and includes an additional variable field counter. In a distributed application such a variable is known as a shared variable. A shared variable is the simplest kind of distributed data structure we can create.
import net.jini.core.entry.*;
/* ----------------------------------------- */
/* */
/* MESSAGE CLASS */
/* */
/* ----------------------------------------- */
public class Message implements Entry {
// Fields
public String content;
public Integer counter;
// Constructors
public Message() {
}
public Message(String content, int initVal) {
this.content = content;
counter = new Integer(initVal);
}
// Methods
public String toString() {
return(content + " read " + counter + " times");
}
public void increment() {
counter = new Integer(counter.intValue() + 1);
}
}
|
Table 1: Message class (based on that contained in Freeman et al., 1999).
Table 2 gives a revised version of the HelloWorld class described previously. The code creates a Message entry and then continualy reads the entry and outputs its fields. Note: that by running this piece of code on its own the output will always be:
Hello World read 0 times
as there is nothing that alters the counter.
/* ------------------------------------------ */
/* */
/* HELLO WORLD 2 */
/* */
/* ------------------------------------------ */
// JavaSpacesUtil package
import JavaSpacesUtils.SpaceAccessor;
// Jini core packages
import net.jini.core.lease.*;
// Jini extension package
import net.jini.space.JavaSpace;
public class HelloWorld2 {
/* ------ METHODS ------ */
/* MAIN */
public static void main(String[] args) {
System.out.println("START");
// try block
try {
// Create Message object entry
System.out.println("Create Message entry");
Message msg = new Message("Hello World",0);
// Get JavaSpace
SpaceAccessor newSpaceAccessor = new
SpaceAccessor("/home/staff5/ra/frans/JavaProgs/" +
"JavaSpaces/JavaSpacesUtils/frans_space.prop");
System.out.println("Get JavaSpace");
JavaSpace space = newSpaceAccessor.getSpace();
// Send Message entry into space
space.write(msg,null,Lease.FOREVER);
// Create a template
Message template = new Message();
// Loop
for(;;) {
// Read entry
Message result = (Message)
space.read(template,null,Long.MAX_VALUE);
// Output, automatically calls toString method
System.out.println(result);
// Delay 1 second
Thread.sleep(1000);
}
}
// Catch block
catch(Exception e) {
e.printStackTrace();
}
}
}
|
Table 2: HelloWorld2 class
The process in Table 2 creates a Message entry and then continualy reads the entry and outputs its fields. We will now create a second process to upadte the counter (Section 4).
The code in Table 3 describes a second process which take a Message object from the space (using a template), increments the counter and writes the revised Message object back into the space.
/* ----------------------------------------- */
/* */
/* HELLO WORLD CLIENT */
/* */
/* ----------------------------------------- */
// JavaSpacesUtil package
import JavaSpacesUtils.SpaceAccessor;
// Jini core packages
import net.jini.core.lease.*;
// Jini extension package
import net.jini.space.JavaSpace;
public class HelloWorldClient {
/* ------ METHODS ------ */
/* MAIN */
public static void main(String[] args) {
// try block
try {
// Get JavaSpace
SpaceAccessor newSpaceAccessor = new
SpaceAccessor("/home/staff5/ra/frans/JavaProgs/" +
"JavaSpaces/JavaSpacesUtils/frans_space.prop");
System.out.println("Get JavaSpace");
JavaSpace space = newSpaceAccessor.getSpace();
// Create a template
Message template = new Message();
// Loop
for(;;) {
// Take entry
Message result = (Message)
space.take(template,null,Long.MAX_VALUE);
// Increment counter
result.increment();
// Write back into space
System.out.println("Increment counter " + result.counter + "+ 1");
space.write(result,null,Lease.FOREVER);
// Delay 1 second
Thread.sleep(1000);
}
}
// Catch block
catch(Exception e) {
e.printStackTrace();
}
}
}
|
Table 3: HelloWorldClient class
Compile the above and make sure that all of the above are in the same directory together with:
Start up the HelloWorld2 application on one machine, and two clients on two other machimes. We should see some output of the form presented in Table 4, 5 and 6.
Table 4: HelloWorld master process |
Table 5: HelloWorldClient Process 1
Table 6: HelloWorldClient process 2 |
Created and maintained by Frans Coenen. Last updated 24 June 2002