System.setSecurityManager(new RMISecurityManager());
// Find the transaction manager
ServiceFinder sf = new ServiceFinder(TransactionManager.class);
TransactionManager txm = (TransactionManager)(sf.getObject());
LeaseRenewalManager lrm = new LeaseRenewalManager();
// Find the JavaSpaces
ServiceFinder sfjs = new ServiceFinder(JavaSpace.class);
JavaSpace js = (JavaSpace) sfjs.getObject();
Conversion request = new Conversion(false);
Conversion result;
while (true) {
Transaction.Created txn = null;
try {
// Take the request in a thread so that if the service
// exits before it writes the answer the request is not
// lost
txn = TransactionFactory.create(txm, Lease.FOREVER);
lrm.renewUntil(txn.lease, Lease.FOREVER, null);
result = (Conversion) js.take(request,
txn.transaction, Long.MAX_VALUE);
// Calculate the answer
try {
result.result = String.valueOf(result.value);
} catch (Exception e) {
// If we can't calculate the answer, we have to
// abort the transaction, but we can go get the
// next request
txn.transaction.abort();
lrm.remove(txn.lease);
continue;
}
// Write the answer
result.done = new Boolean(true);
js.write(result, txn.transaction, 5 * 60 * 1000);
txn.transaction.commit();
lrm.remove(txn.lease);
} catch (Exception e) {
// These exceptions mean that we couldn't talk to the
// space or transaction manager, which we can't
// recover from.
System.out.println("Unrecoverable error" + e);
System.exit(-1);
}
}