Class Locker
- java.lang.Object
-
- org.apache.curator.framework.recipes.locks.Locker
-
- All Implemented Interfaces:
AutoCloseable
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 Summary
Constructors Constructor Description Locker(InterProcessLock lock)
Locker(InterProcessLock lock, long timeout, TimeUnit unit)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
acquireLock(InterProcessLock lock)
protected boolean
acquireLock(InterProcessLock lock, long timeout, TimeUnit unit)
void
close()
protected void
releaseLock()
-
-
-
Constructor Detail
-
Locker
public Locker(InterProcessLock lock, long timeout, TimeUnit unit) throws Exception
- Parameters:
lock
- a lock implementation (e.g.InterProcessMutex
,InterProcessSemaphoreV2
, etc.)timeout
- max timeout to acquire lockunit
- time unit of timeout- Throws:
Exception
- Curator errors orTimeoutException
if the lock cannot be acquired within the timeout
-
Locker
public Locker(InterProcessLock lock) throws Exception
- Parameters:
lock
- a lock implementation (e.g.InterProcessMutex
,InterProcessSemaphoreV2
, etc.)- Throws:
Exception
- errors
-
-
Method Detail
-
close
public void close() throws Exception
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-
acquireLock
protected void acquireLock(InterProcessLock lock) throws Exception
- Throws:
Exception
-
acquireLock
protected boolean acquireLock(InterProcessLock lock, long timeout, TimeUnit unit) throws Exception
- Throws:
Exception
-
-