ENTERPRISE JAVAHow to Use Jini Distributed Leasing

ENTERPRISE JAVA
How to Use Jini Distributed Leasing

Kathy Kozel

Listing 5. Client using LeaseRenewalManager.


import java.rmi.*;
import net.jini.core.lease.*;
import net.jini.lease.*;

// version 3 client—uses LeaseRenewalManager to renew lease

public class DogClient3 implements LeaseListener {

      private Confirmation confirm;
      private Trainable myDog = new Dog();
      private Kennel kennel;
      
      //private Thread renewalThread; 
      // we don't need our own renewal thread now
      // since LeaseRenewalManager will run one for us
     
      private long lengthOfStay = 1000 * 60 * 5; 
      private long netTravelBuffer = (1000 * 30);  
      //add a lease renewal manager to, well, you know—manage
      // lease renewal!
      LeaseRenewalManager leaseMgr;

    public static void main (String args[]) {
           DogClient3 client = new DogClient3();
           client.startUp();
      }
     
    public void startUp() {
         //get a reference to a remote Kennel (using RMIregistry or 
         // Jini lookup)
         // this example uses RMI to keep the example short

       try {
            kennel = (Kennel) Naming.lookup("rmi://127.0.0.1:1099/Kennel");
            System.out.println("got the kennel from registry");
            confirm = kennel.bookRoom(myDog, lengthOfStay);

            System.out.println("We got a lease for " +
             confirm.getLease().getExpiration());
            System.out.println("we want the dog to stay in for " +
             lengthOfStay);

            //NOW START THE LEASE RENEWAL MANAGER AND GIVE IT A
            //Lease!
           long leaseUntilTime = (lengthOfStay + netTravelBuffer + 
           System.currentTimeMillis());
           leaseMgr = new LeaseRenewalManager(confirm.getLease(),
            leaseUntilTime, this);

           } catch(Exception e) {
               e.printStackTrace();
             }

        // now we assume the lease manager is happily renewing the
        // leases for us and we can now just wait and get the dog out
 
         try {
            Thread.sleep(lengthOfStay);
          } catch(InterruptedException e) {System.out.println("thread
          interrupted!");}

         // now get the dog
         retrieveDog();
       
      } // close startUp

   public void notify(LeaseRenewalEvent ev) {
        System.out.println("uh oh! Got a lease renewal event from
         lease renewal manager");
        System.out.println("something must have gone wrong in lease
         renewal");
        ev.getException().printStackTrace();
        // code should go here to deal with the problem
        // and probably try to take the dog out immediately
    }

   protected void retrieveDog() {
        try {
          myDog = (Dog) kennel.takeTrainableHome(confirm.getID(),
           confirm.getRoomNum());
          System.out.println("I got my dog back!");
         }
         catch(Exception e) {
           System.out.println("couldn't get my dog!");
           e.printStackTrace();
         }
      
     }
  } // close client