Package com.coditory.sherlock
Interface DistributedLock
-
- All Known Implementing Classes:
DistributedLockMock
public interface DistributedLockA distributed lock.- See Also:
Sherlock
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classDistributedLock.AcquireAndExecuteResultstatic classDistributedLock.ReleaseAndExecuteResult
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description booleanacquire()Try to acquire the lock.booleanacquire(java.time.Duration duration)Try to acquire the lock for a given duration.default DistributedLock.AcquireAndExecuteResultacquireAndExecute(java.lang.Runnable action)Acquire a lock and release it after action is executed.default DistributedLock.AcquireAndExecuteResultacquireAndExecute(java.time.Duration duration, java.lang.Runnable action)Acquire a lock for specific duration and release it after action is executed.booleanacquireForever()Try to acquire the lock without expiring date.default DistributedLock.AcquireAndExecuteResultacquireForeverAndExecute(java.lang.Runnable action)Acquire a lock for specific duration and release it after action is executed.java.lang.StringgetId()Return the lock id.booleanrelease()Try to release the lock.default DistributedLock.ReleaseAndExecuteResultreleaseAndExecute(java.lang.Runnable action)Run the action if lock is released.
-
-
-
Method Detail
-
getId
java.lang.String getId()
Return the lock id.- Returns:
- the lock id
-
acquire
boolean acquire()
Try to acquire the lock. Lock is acquired for a pre configured duration.- Returns:
- true if lock is acquired
-
acquire
boolean acquire(java.time.Duration duration)
Try to acquire the lock for a given duration.- Parameters:
duration- how much time must pass for the acquired lock to expire- Returns:
- true if lock is acquired
-
acquireForever
boolean acquireForever()
Try to acquire the lock without expiring date.It is potentially dangerous. Lookout for a situation when the lock owning instance goes down with out releasing the lock.
- Returns:
- true if lock is acquired
-
release
boolean release()
Try to release the lock.- Returns:
- true if lock was released by this method invocation. If lock has expired or was released earlier then false is returned.
-
acquireAndExecute
default DistributedLock.AcquireAndExecuteResult acquireAndExecute(java.lang.Runnable action)
Acquire a lock and release it after action is executed.This is a helper method that makes sure the lock is released when action finishes successfully or throws an exception.
- Parameters:
action- to be executed when lock is acquired- Returns:
DistributedLock.AcquireAndExecuteResult- See Also:
acquire()
-
acquireAndExecute
default DistributedLock.AcquireAndExecuteResult acquireAndExecute(java.time.Duration duration, java.lang.Runnable action)
Acquire a lock for specific duration and release it after action is executed.This is a helper method that makes sure the lock is released when action finishes successfully or throws an exception.
- Parameters:
duration- how much time must pass to release the lockaction- to be executed when lock is acquired- Returns:
DistributedLock.AcquireAndExecuteResult- See Also:
acquire(Duration)
-
acquireForeverAndExecute
default DistributedLock.AcquireAndExecuteResult acquireForeverAndExecute(java.lang.Runnable action)
Acquire a lock for specific duration and release it after action is executed.This is a helper method that makes sure the lock is released when action finishes successfully or throws an exception.
- Parameters:
action- to be executed when lock is acquired- Returns:
DistributedLock.AcquireAndExecuteResult- See Also:
acquireForever()
-
releaseAndExecute
default DistributedLock.ReleaseAndExecuteResult releaseAndExecute(java.lang.Runnable action)
Run the action if lock is released.- Parameters:
action- to be executed when lock is released- Returns:
DistributedLock.ReleaseAndExecuteResult- See Also:
release()
-
-