Class LockItem
- java.lang.Object
-
- com.amazonaws.services.dynamodbv2.LockItem
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class LockItem extends Object implements Closeable
A lock that has been successfully acquired. Provides APIs for releasing, heartbeating, and extending the lock.- Author:
- Sasha Slutsker
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanamIAboutToExpire()Returns whether or not the lock is entering the "danger zone" time period.voidclose()Releases the lock for others to use.voidensure(long leaseDurationToEnsure, TimeUnit timeUnit)Ensures that this owner has the lock for a specified period of time.booleanequals(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()booleangetDeleteLockItemOnClose()Returns a boolean indicating whether the lock should be deleted from DynamoDB after release.longgetLeaseDuration()Returns the amount of time that the client has this lock for, which can be kept up to date by callingsendHeartbeat.longgetLookupTime()Returns the last time this lock was updated.StringgetOwnerName()Returns the name of the owner that owns this lock.StringgetPartitionKey()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.)StringgetRecordVersionNumber()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.inthashCode()Hash code of just the (key, sortKey, ownerName) as that is what uniquely identifies this lock.booleanisExpired()Returns whether or not the lock is expired, based on the lease duration and when the last heartbeat was sent.voidsendHeartBeat()Sends a heartbeat to indicate that the given lock is still being worked on.StringtoString()Returns a string representation of this lock.voidupdateLookUpTime(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 callingsendHeartbeat.- 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
toString
public String toString()
Returns a string representation of this lock.
-
hashCode
public int hashCode()
Hash code of just the (key, sortKey, ownerName) as that is what uniquely identifies this lock.
-
equals
public boolean equals(Object other)
Returns if two locks have the same (key, sortKey, ownerName)
-
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 usingcreateHeartbeatBackgroundThread=true when setting up this object, then this method is unnecessary, because the background thread will be periodically calling it and sending heartbeats. However, ifcreateHeartbeatBackgroundThread=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
leaseDurationto that value and send a heartbeat, such that the lock will expire no sooner than afterleaseDurationelapses. Otherwise, this method will do nothing.This method is not required if using heartbeats, because the client could simply call
isExpiredbefore 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
LockNotGrantedExceptionif it does not currently hold the lock.- Parameters:
leaseDurationToEnsure- The amount of time to ensure that the lease is granted fortimeUnit- 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:
trueif the lock has been released or the lock's lease has entered the "danger zone" *falseif the lock has not been released and the lock has not yet entered the "danger zone"- Throws:
SessionMonitorNotSetException- when the SessionMonitor is not setIllegalStateException- when the lock is already released
-
-