Sample JavaSpaces code

The following code indicates how to write the Message object and how to use it in your code:

//Message.java
package org.hr.communication;
import net.jini.core.entry.*;
public class Message implements Entry {
public String details;
public Message() {
}
public Message(String details) {
this.details = details;
}
public String toString() {
return "MessageContent: " + details;
}
}

In the following code, the client makes use of the Message object to store data in the space and then retrieve it as and when required:

package org.hr.communication;
import net.jini.space.JavaSpace;
public class SpaceUser {
public static void main(String args[]) {
try {
Message message = new Message();
message.content = "Hi ";
//Searching for Java Space
Lookup searcher = new Lookup(JavaSpace.class);
JavaSpace jSpace = (JavaSpace) searcher.getService();
//A Java Space is found
//Writing a message to space
jSpace.write(message, null, 60 * 60 * 500);
Message tmpl = new Message();
//Reading a message from the space
Message rslt = (Message) jSpace.read(tmpl, null,
Long.MAX_VALUE);
//Print the message content
System.out.println("The message read is: " + rslt.content);
} catch (Exception e) {
e.printStackTrace();
}
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset