Skip to content

Lock

Purpose

Provides cluster wide locking methods.

Methods

Binding name:

  • p6.lock

isLocked

Test if the named cluster wide lock is locked

Syntax

boolean p6.lock.isLocked(String lockName)
Example
println p6.lock.isLocked('myLock')

with

Acquire/lock the named cluster wide lock within the specified acquire timeout and execute the supplied critical section

Syntax

boolean p6.lock.with(String lockName, long acquireTimeoutMillis, Closure<Void> criticalSection)

Info

The lock is guaranteed to be unlocked when this method returns

If lock acquisition fails this method will return false.

Example
def acquiredLock = p6.lock.with('myLock', 1000){
    // The lock will be released once this code block completes
    println 'I have the lock!'
}
// Failure to acquire the lock within the given timeout
if(!acquiredLock){
    println 'I failed to acquire the lock'
}

p6.lock.destroy('myLock')

tryLeased

Acquire/lock the named cluster wide lock within the specified acquire timeout. The supplied lease timeout ensures the lock is released (unlocked) when the lease time expires

Syntax

Lock p6.lock.tryLeased(String lockName, long acquireTimeoutMillis, long leaseTimeoutMillis)

Info

There is no need to unlock the acquired lock as it will automatically unlock. However, the returned Lock may be unlocked before this time if required.

Example
def lock = p6.lock.tryLeased('testLeased', 500, 1500)
if(null == lock){
    println 'I failed to acquire the leased lock'
}
// The lock wil be held until the lease timer expires or lock.unlock() is called
lock.unlock()

p6.lock.destroy('testLeased')

destroy

Destroy the named cluster wide lock and release any associated resources.

Syntax

void p6.lock.destroy(String lockName)
Example
p6.lock.destroy('myLock')