public class Locks extends Object implements sirius.kernel.health.metrics.MetricProvider
These locks can either be distributed (via SQL or REDIS) or held locally. The implementation is provided via a
LockManager
.
Modifier and Type | Field and Description |
---|---|
static String |
FRAMEWORK_LOCKS
Names the framework which must be enabled to activate the locks feature.
|
static sirius.kernel.health.Log |
LOG |
Constructor and Description |
---|
Locks() |
Modifier and Type | Method and Description |
---|---|
void |
gather(sirius.kernel.health.metrics.MetricsCollector collector) |
List<LockInfo> |
getLocks()
Returns a list of all currently held locks.
|
boolean |
isLocked(String lock)
Determines if the given lock is currently locked.
|
boolean |
tryLock(String lockName,
Duration acquireTimeout)
Tries to acquire the given lock in the given timeslot.
|
void |
tryLocked(String lock,
Duration acquireTimeout,
Runnable lockedTask)
Boilerplate method to perform the given task while holding the given lock.
|
void |
unlock(String lock)
Releases the lock.
|
void |
unlock(String lock,
boolean force)
Releases the given lock.
|
public static final String FRAMEWORK_LOCKS
public static final sirius.kernel.health.Log LOG
public boolean tryLock(@Nonnull String lockName, @Nullable Duration acquireTimeout)
A sane value for the timeout might be in the range of 5-50s, highly depending on the algorithm being protected by the lock. If the value is null, no retries will be performed.
lockName
- the name of the lock to acquireacquireTimeout
- the max duration during which retires will be performedpublic void tryLocked(@Nonnull String lock, @Nullable Duration acquireTimeout, @Nonnull Runnable lockedTask)
See tryLock(String, Duration)
for details on acquiring a lock.
If the lock cannot be acquired, nothing will happen (neighter the task will be execute nor an exception will be thrown).
lock
- the name of the lock to acquireacquireTimeout
- the max duration during which retires will be performedlockedTask
- the task to execute while holding the given lock. The task will not be executed if the
lock cannot be acquired within the given periodpublic boolean isLocked(@Nonnull String lock)
lock
- the lock to checkpublic void unlock(String lock)
lock
- the lock to releasepublic void unlock(String lock, boolean force)
lock
- the lock to releaseforce
- if true, the lock will even be released if it is held by another node. This is a very
dangerous operation and should only be used by maintenance and management tools.public void gather(sirius.kernel.health.metrics.MetricsCollector collector)
gather
in interface sirius.kernel.health.metrics.MetricProvider
Copyright © 2018. All rights reserved.