previous chapter table of contents next chapter

Requesting and Receiving Leases

Leases are requested for a period of time. In Jini, a common use of leasing is for a service provider to request that a copy of the service be kept on a lookup service for a certain length of time, for delivery to clients on request. The service provider requests a time in the ServiceRegistrar 's register() method. Two special values of the time are

  • Lease.ANY ”the service lets the lookup service decide on the time
  • Lease.FOREVER ”the request is for a lease that never expires

The lookup service acts as the granter of the lease and decides how long it will actually create the lease for. (The lookup service from Sun typically sets the lease time as only five minutes.) Once it has done that, it will attempt to ensure that the request is honored for that period of time. The lease is returned to the service and is accessible through the getLease() method of the ServiceRegistration object. These objects are shown in Figure 7-1. The following are typical calls to register the service and then find the lease:

 ServiceRegistration reg = registrar.register(); Lease lease = reg.getLease(); 

Figure 7-1: Objects in a leased system

The principal methods of the Lease object are these:

 package net.jini.core; public interface Lease {     void cancel() throws                   UnknownLeaseException,                   java.rmi.RemoteException;     long getExpiration();     void renew(long duration) throws                   LeaseDeniedException,                   UnknownLeaseException,                   java.rmi.RemoteException; } 

The expiration value returned from getExpiration() is the time in milliseconds since the beginning of the epoch (the same as in System.currentTimeMillis() ). To find the amount of time still remaining from the present, the current time can be subtracted from this, as follows :

 long duration = lease.getExpiration() - System.currentTimeMillis(); 


A service can cancel its lease by using cancel() . The lease communicates back to the lease management system on the lookup service, which cancels storage of the service.


When a lease expires, it does so silently. That is, the lease granter (the lookup service) will not inform the lease holder (the service) that it has expired . While it might seem nice to get warning of a lease expiring so that it can be renewed, this would have to be done in advance of the expiration ("I'm just about to expire; please renew me quickly!") but this would complicate the leasing system and not be completely reliable anyway (for example, how far in advance is soon enough?).

Instead, it is up to the service provider to call renew() before the lease expires if it wishes the lease to continue. The parameter for renew() is in milliseconds, and represents an extra duration from now. This is in contrast to the expiration time returned from getExpiration() , which is measured since the epoch.


A Programmer[ap]s Guide to Jini Technology
A Programmer[ap]s Guide to Jini Technology
ISBN: 1893115801
Year: 2000
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net