Package com.coditory.sherlock
Interface DistributedLock
- All Known Implementing Classes:
DistributedLockMock
public interface DistributedLock
A distributed lock.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionboolean
acquire()
Tries to acquire the lock.boolean
Tries to acquire the lock for a given duration.boolean
Tries to acquire the lock without expiration date.@NotNull String
getId()
Returns the lock id.boolean
release()
Tries to release the lock.default com.coditory.sherlock.connector.AcquireResult
Tries to acquire the lock and releases it after action is executed.default com.coditory.sherlock.connector.AcquireResult
Acquire a lock for specific duration and release it after action is executed.default <T> com.coditory.sherlock.connector.AcquireResultWithValue
<T> Tries to acquire the lock for a given duration and releases it after action is executed.default <T> com.coditory.sherlock.connector.AcquireResultWithValue
<T> Tries to acquire the lock and releases it after action is executed.
-
Method Details
-
getId
Returns the lock id.- Returns:
- the lock id
-
acquire
boolean acquire()Tries to acquire the lock. Lock is acquired for a pre-configured duration. I lock is not released manually, it becomes released after expiration time.- Returns:
- true if lock is acquired
-
acquire
Tries to acquire the lock for a given duration. If lock is not released manually, it becomes released after expiration time.- Parameters:
duration
- lock expiration time when release is not executed- Returns:
- true if lock is acquired
-
acquireForever
boolean acquireForever()Tries to acquire the lock without expiration date.It is potentially dangerous. Lookout for a situation when the lock owning instance goes down without releasing the lock.
- Returns:
- true if lock is acquired
-
release
boolean release()Tries 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.
-
runLocked
default <T> com.coditory.sherlock.connector.AcquireResultWithValue<T> runLocked(@NotNull @NotNull Supplier<? extends T> supplier) Tries to acquire the lock and releases it after action is executed.- Type Parameters:
T
- type emitted when lock is acquired- Parameters:
supplier
- executed when lock is acquired- Returns:
-
invalid reference
AcquireResultWithValue#acquiredResult(T)
- See Also:
-
runLocked
default com.coditory.sherlock.connector.AcquireResult runLocked(@NotNull @NotNull Runnable runnable) Tries to acquire the lock and releases it after action is executed.- Parameters:
runnable
- executed when lock is acquired- Returns:
AcquireResult.acquiredResult()
if lock was acquired- See Also:
-
runLocked
default com.coditory.sherlock.connector.AcquireResult runLocked(@NotNull @NotNull Duration duration, @NotNull @NotNull Runnable runnable) 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 lockrunnable
- to be executed when lock is acquired- Returns:
AcquireResult.acquiredResult()
if lock was acquired- See Also:
-
runLocked
default <T> com.coditory.sherlock.connector.AcquireResultWithValue<T> runLocked(@NotNull @NotNull Duration duration, @NotNull @NotNull Supplier<? extends T> supplier) Tries to acquire the lock for a given duration and releases it after action is executed.- Type Parameters:
T
- type emitted when lock is acquired- Parameters:
duration
- lock expiration time when release is not executedsupplier
- executed when lock is acquired- Returns:
-
invalid reference
AcquireResultWithValue#acquiredResult(T)
- See Also:
-