|
The Nature of JavaSpacesJavaspaces have been compared with technologies such as message queues and databases. It is important to understand that whilst JavaSpaces can be used to perform some of the tasks supported by these other technologies, they should be treated very differently. JavaSpaces are best considered as distributed shared memory with additional features which provide transactional integrity and support for the handling of failure. They are best suited to problems which can be modelled as flows of objects such as:
JavaSpaces provide a simple API for the manipulation of
Note also that the objects referenced in an
public class SimpleEntry implements net.jini.core.entry.Entry {
public Foo aFoo;
}
public class Foo implements java.io.Serializable {
private Integer anInteger;
}
An instance of
// This is a basic Entry implementation - only public fields are stored
//
public class DummyEntry implements Entry {
public String theName;
public String theOtherField;
public DummyEntry() {
}
public DummyEntry(String aName) {
theName = aName;
}
public String toString() {
return theName;
}
// JavaSpaces doesn't use this method - it's just here for completeness....
public boolean equals(Object anObject) {
if ((anObject != null) && (anObject instanceof DummyEntry)) {
DummyEntry myEntry = (DummyEntry) anObject;
if (myEntry.theName == null)
return (myEntry.theName == theName);
else
return ((DummyEntry) anObject).theName.equals(theName);
}
return false;
}
}
// Lookup JavaSpace and then......
// "Invent" the value we're looking for - normally, you'd get input from the user
// or whatever....
Integer myValue =
new Integer(theRNG.nextInt(thePoolSize));
// Build template
Entry myTemplate = new DummyEntry(myValue.toString());
// We'll do this transactionally - we want txn to timeout if it takes more than 5 seconds.
Transaction.Created myTxnC =
TransactionFactory.create(theTxnMgr, 5000);
Transaction myTxn = myTxnC.transaction;
// Ask space to check for match, delete from space and return it to us
// The take actually locks the Entry, deletion won't occur 'til we commit
// If we take too long to execute or we fail, the transaction lease will expire and
// the transaction is automatically aborted, which makes our taken Entry available again
Entry myResult = theSpace.take(myTemplate, myTxn, aTimeout);
// If we got it.....
if (myResult != null) {
// Make some changes......
// Write it back
theSpace.write(myResult, myTxn, Lease.FOREVER);
}
// Commit the transaction
myTxn.commit();
Further Reading |