Skip to content

Lock

Purpose

Provides cluster wide locking methods.

Methods

Binding name:

  • p6.lock

Method: boolean isLocked(String lockName)

Test if the named cluster wide lock is locked


Method: boolean with(String lockName, long acquireTimeoutMillis, Closure<Void> criticalSection)

Acquire/lock the named cluster wide lock within the specified acquire timeout and execute the supplied critical section The lock is guaranteed to be unlocked when this method returns

If lock acquisition fails this method will return false.


Method: Lock tryLeased(String lockName, long acquireTimeoutMillis, long leaseTimeoutMillis)

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

Note

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.


Method: void destroy(String lockName)

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

Examples

println p6.lock.isLocked('myLock')
p6.lock.destroy('myLock')



// Wait up to a second to acquire the lock
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')



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')