Class LockItem

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean amIAboutToExpire()
      Returns whether or not the lock is entering the "danger zone" time period.
      void close()
      Releases the lock for others to use.
      void ensure​(long leaseDurationToEnsure, TimeUnit timeUnit)
      Ensures that this owner has the lock for a specified period of time.
      boolean equals​(Object other)
      Returns if two locks have the same (key, sortKey, ownerName)
      Map<String,​software.amazon.awssdk.services.dynamodb.model.AttributeValue> getAdditionalAttributes()
      Returns the additional attributes that can optionally be stored alongside the lock.
      Optional<ByteBuffer> getData()  
      boolean getDeleteLockItemOnClose()
      Returns a boolean indicating whether the lock should be deleted from DynamoDB after release.
      long getLeaseDuration()
      Returns the amount of time that the client has this lock for, which can be kept up to date by calling sendHeartbeat.
      long getLookupTime()
      Returns the last time this lock was updated.
      String getOwnerName()
      Returns the name of the owner that owns this lock.
      String getPartitionKey()
      Returns the key associated with this lock, which is unique for every lock (unless the lock has a sort key, in which case key + sortKey is unique.)
      String getRecordVersionNumber()
      Returns the current record version number of the lock in DynamoDB.
      Optional<String> getSortKey()
      Returns the sort key associated with the lock, if there is one.
      int hashCode()
      Hash code of just the (key, sortKey, ownerName) as that is what uniquely identifies this lock.
      boolean isExpired()
      Returns whether or not the lock is expired, based on the lease duration and when the last heartbeat was sent.
      void sendHeartBeat()
      Sends a heartbeat to indicate that the given lock is still being worked on.
      String toString()
      Returns a string representation of this lock.
      void updateLookUpTime​(long lastUpdateOfLock)
      Updates the last updated time of the lock.
    • Method Detail

      • getPartitionKey

        public String getPartitionKey()
        Returns the key associated with this lock, which is unique for every lock (unless the lock has a sort key, in which case key + sortKey is unique.)
        Returns:
        The key identifying the lock.
      • getSortKey

        public Optional<String> getSortKey()
        Returns the sort key associated with the lock, if there is one.
        Returns:
        The sort key for the lock.
      • getData

        public Optional<ByteBuffer> getData()
        Returns:
        Returns the data associated with the lock, which is optional.
      • getAdditionalAttributes

        public Map<String,​software.amazon.awssdk.services.dynamodb.model.AttributeValue> getAdditionalAttributes()
        Returns the additional attributes that can optionally be stored alongside the lock.
        Returns:
        The additional attributes that can optionally be stored alongside the lock.
      • getOwnerName

        public String getOwnerName()
        Returns the name of the owner that owns this lock.
        Returns:
        The owner name
      • getLookupTime

        public long getLookupTime()
        Returns the last time this lock was updated. Note that this will use LockClientUtils.millisecondTime() so it does not represent an actual absolute time.
        Returns:
        The last time this lock was updated
      • getRecordVersionNumber

        public String getRecordVersionNumber()
        Returns the current record version number of the lock in DynamoDB. This is what tells the lock client when the lock is stale.
        Returns:
        The current record version number
      • getLeaseDuration

        public long getLeaseDuration()
        Returns the amount of time that the client has this lock for, which can be kept up to date by calling sendHeartbeat.
        Returns:
        the lease duration of this lock item
      • getDeleteLockItemOnClose

        public boolean getDeleteLockItemOnClose()
        Returns a boolean indicating whether the lock should be deleted from DynamoDB after release.
        Returns:
        true if the lock should be deleted, false if it should not.
      • close

        public void close()
        Releases the lock for others to use.
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
      • toString

        public String toString()
        Returns a string representation of this lock.
        Overrides:
        toString in class Object
      • hashCode

        public int hashCode()
        Hash code of just the (key, sortKey, ownerName) as that is what uniquely identifies this lock.
        Overrides:
        hashCode in class Object
      • equals

        public boolean equals​(Object other)
        Returns if two locks have the same (key, sortKey, ownerName)
        Overrides:
        equals in class Object
      • isExpired

        public boolean isExpired()
        Returns whether or not the lock is expired, based on the lease duration and when the last heartbeat was sent.
        Returns:
        True if the lock is expired, false otherwise
      • sendHeartBeat

        public void sendHeartBeat()
        Sends a heartbeat to indicate that the given lock is still being worked on. If using createHeartbeatBackgroundThread=true when setting up this object, then this method is unnecessary, because the background thread will be periodically calling it and sending heartbeats. However, if createHeartbeatBackgroundThread=false, then this method must be called to instruct DynamoDB that the lock should not be expired.

        This is equivalent to calling lockClient.sendHeartbeat(lockItem)

      • ensure

        public void ensure​(long leaseDurationToEnsure,
                           TimeUnit timeUnit)

        Ensures that this owner has the lock for a specified period of time. If the lock will expire in less than the amount of time passed in, then this method will set the leaseDuration to that value and send a heartbeat, such that the lock will expire no sooner than after leaseDuration elapses. Otherwise, this method will do nothing.

        This method is not required if using heartbeats, because the client could simply call isExpired before every operation to ensure that it still has the lock. However, it is possible for the client to instead call this method before executing every operation if they all require different lengths of time, and the client wants to ensure it always has enough time.

        This method will throw a LockNotGrantedException if it does not currently hold the lock.

        Parameters:
        leaseDurationToEnsure - The amount of time to ensure that the lease is granted for
        timeUnit - The time unit for the leaseDuration
      • updateLookUpTime

        public void updateLookUpTime​(long lastUpdateOfLock)
        Updates the last updated time of the lock.
        Parameters:
        lastUpdateOfLock - - Time to update the lock with
      • amIAboutToExpire

        public boolean amIAboutToExpire()
        Returns whether or not the lock is entering the "danger zone" time period.
        Returns:
        true if the lock has been released or the lock's lease has entered the "danger zone" * false if the lock has not been released and the lock has not yet entered the "danger zone"
        Throws:
        SessionMonitorNotSetException - when the SessionMonitor is not set
        IllegalStateException - when the lock is already released