Class ContractLease<T>

  • All Implemented Interfaces:
    Lease<T>

    class ContractLease<T>
    extends java.lang.Object
    implements Lease<T>
    The ContractLease is used to maintain contracts by using a controller object. This will invoke the controller with the contract when a lease operation is performed. A lease is renewed by changing the contract duration and passing that to the controller which will reestablish the expiry time for it.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private Contract<T> contract
      This is the contract object representing the lease.
      private ContractController<T> handler
      This is the controller object used to handle contracts.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cancel()
      This will cancel the lease and release the resource.
      long getExpiry​(java.util.concurrent.TimeUnit unit)
      Determines the duration remaining before the lease expires.
      T getKey()
      Provides the key for the resource that this lease represents.
      void renew​(long duration, java.util.concurrent.TimeUnit unit)
      This ensures that the leased resource is maintained for the specified number of TimeUnit seconds.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • handler

        private final ContractController<T> handler
        This is the controller object used to handle contracts.
      • contract

        private final Contract<T> contract
        This is the contract object representing the lease.
    • Constructor Detail

      • ContractLease

        public ContractLease​(ContractController<T> handler,
                             Contract<T> contract)
        Constructor for the ContractLease object. This is used to create a lease which will maintain a contract using a controller object. Lease renewals are performed by changing the expiry duration on the contract and notifying the controller.
        Parameters:
        handler - this is used to manage the contract expiration
        contract - this is the contract representing the lease
    • Method Detail

      • getExpiry

        public long getExpiry​(java.util.concurrent.TimeUnit unit)
                       throws LeaseException
        Determines the duration remaining before the lease expires. The expiry is given as the number of TimeUnit seconds remaining before the lease expires. If this value is negative it should be assumed that the lease has expired.
        Specified by:
        getExpiry in interface Lease<T>
        Parameters:
        unit - this is the time unit used for the duration
        Returns:
        the duration remaining within this lease instance
        Throws:
        LeaseException - if the lease expiry has passed
      • renew

        public void renew​(long duration,
                          java.util.concurrent.TimeUnit unit)
                   throws LeaseException
        This ensures that the leased resource is maintained for the specified number of TimeUnit seconds. Allowing the duration unit to be specified enables the lease system to maintain a resource with a high degree of accuracy. The accuracy of the leasing system is dependant on how long it takes to clean the resource associated with the lease.
        Specified by:
        renew in interface Lease<T>
        Parameters:
        duration - this is the length of time to renew for
        unit - this is the time unit used for the duration
        Throws:
        LeaseException - if the expiry has been passed
      • cancel

        public void cancel()
                    throws LeaseException
        This will cancel the lease and release the resource. This has the same effect as the renew method with a zero length duration. Once this has been called the Cleaner used should be notified immediately. If the lease has already expired this throws an exception.
        Specified by:
        cancel in interface Lease<T>
        Throws:
        LeaseException - if the expiry has been passed
      • getKey

        public T getKey()
        Provides the key for the resource that this lease represents. This can be used to identify the resource should the need arise. Also, this provides a convenient means of identifying leases when using or storing it as an Object.
        Specified by:
        getKey in interface Lease<T>
        Returns:
        this returns the key for the resource represented