FileDocCategorySizeDatePackage
ConvertServiceImpl.javaAPI DocExample5548Thu Mar 16 11:52:18 GMT 2000None

ConvertServiceImpl

public class ConvertServiceImpl extends UnicastRemoteObject implements Runnable, ConvertServiceProxy

Fields Summary
private ServerLandlord
lord
private ServerDelivery
sender
private JavaSpace
js
private ServiceFinder
txsf
Constructors Summary
public ConvertServiceImpl()

        lord = new ServerLandlord();
        sender = new ServerDelivery(this, lord);

        // Get the JavaSpaces and Transaction Manager now so that
        // the thread doesn't have to deal with exceptions if they
        // can't be found
        try {
            ServiceFinder sf = new ServiceFinder(JavaSpace.class);
            js = (JavaSpace) sf.getObject();
            txsf = new ServiceFinder(TransactionManager.class);
        } catch (IOException ioe) {
            throw new RemoteException("Can't find java space", ioe);
        }

        // Start the thread to take and convert requests
        new Thread(this).start();
    
Methods Summary
public ConvertServiceRegistrationgetInstance(long duration)

        Hashtable ht = new Hashtable(13);
        return new ConvertServiceRegistrationImpl(js, lord.newLease(ht, duration));
    
public static voidmain(java.lang.String[] args)

        System.setSecurityManager(new RMISecurityManager());
        String[] groups = new String[] { "" };

        // Create the instance of the service; the JoinManager will
        // register it and renew its leases with the lookup service
        ConvertServiceImpl csi = (ConvertServiceImpl) new ConvertServiceImpl();
        JoinManager manager = new JoinManager(csi, null, groups,
                                              null, null, null);
    
public voidrun()

        TransactionManager txm = (TransactionManager) txsf.getObject();
        LeaseRenewalManager lrm = new LeaseRenewalManager();

        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);

                // Get a request (one with the done field == false)
                result = (Conversion) js.take(request,
                                        txn.transaction, Long.MAX_VALUE);

                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, Long.MAX_VALUE);

                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); 
                return;
            }
        }
    
public EventRegistrationtrackConversions(long duration, RemoteEventListener rel, java.rmi.MarshalledObject key)

        return sender.addListener(rel, duration, key);