public class Locker extends Object implements AutoCloseable
Utility for safely acquiring a lock and releasing it using Java 7's try-with-resource feature.
Canonical usage:
InterProcessMutex mutex = new InterProcessMutex(...) // or any InterProcessLock
try ( Locker locker = new Locker(mutex, maxTimeout, unit) )
{
// do work
}
Constructor and Description |
---|
Locker(InterProcessLock lock) |
Locker(InterProcessLock lock,
long timeout,
TimeUnit unit) |
Modifier and Type | Method and Description |
---|---|
protected void |
acquireLock(InterProcessLock lock) |
protected boolean |
acquireLock(InterProcessLock lock,
long timeout,
TimeUnit unit) |
void |
close() |
protected void |
releaseLock() |
public Locker(InterProcessLock lock, long timeout, TimeUnit unit) throws Exception
lock
- a lock implementation (e.g. InterProcessMutex
, InterProcessSemaphoreV2
, etc.)timeout
- max timeout to acquire lockunit
- time unit of timeoutException
- Curator errors or TimeoutException
if the lock cannot be acquired within the timeoutpublic Locker(InterProcessLock lock) throws Exception
lock
- a lock implementation (e.g. InterProcessMutex
, InterProcessSemaphoreV2
, etc.)Exception
- errorspublic void close() throws Exception
close
in interface AutoCloseable
Exception
protected void acquireLock(InterProcessLock lock) throws Exception
Exception
protected boolean acquireLock(InterProcessLock lock, long timeout, TimeUnit unit) throws Exception
Exception
Copyright © 2011–2023 The Apache Software Foundation. All rights reserved.