Class InterProcessMutex
- java.lang.Object
-
- org.apache.curator.framework.recipes.locks.InterProcessMutex
-
- All Implemented Interfaces:
InterProcessLock,Revocable<InterProcessMutex>
public class InterProcessMutex extends java.lang.Object implements InterProcessLock, Revocable<InterProcessMutex>
A re-entrant mutex that works across JVMs. Uses Zookeeper to hold the lock. All processes in all JVMs that use the same lock path will achieve an inter-process critical section. Further, this mutex is "fair" - each user will get the mutex in the order requested (from ZK's point of view)
-
-
Constructor Summary
Constructors Constructor Description InterProcessMutex(org.apache.curator.framework.CuratorFramework client, java.lang.String path)InterProcessMutex(org.apache.curator.framework.CuratorFramework client, java.lang.String path, LockInternalsDriver driver)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidacquire()Acquire the mutex - blocking until it's available.booleanacquire(long time, java.util.concurrent.TimeUnit unit)Acquire the mutex - blocks until it's available or the given time expires.protected byte[]getLockNodeBytes()protected java.lang.StringgetLockPath()java.util.Collection<java.lang.String>getParticipantNodes()Return a sorted list of all current nodes participating in the lockbooleanisAcquiredInThisProcess()Returns true if the mutex is acquired by a thread in this JVMbooleanisOwnedByCurrentThread()Returns true if the mutex is acquired by the calling threadvoidmakeRevocable(RevocationListener<InterProcessMutex> listener)Make the lock revocable.voidmakeRevocable(RevocationListener<InterProcessMutex> listener, java.util.concurrent.Executor executor)Make the lock revocable.voidrelease()Perform one release of the mutex if the calling thread is the same thread that acquired it.
-
-
-
Constructor Detail
-
InterProcessMutex
public InterProcessMutex(org.apache.curator.framework.CuratorFramework client, java.lang.String path)- Parameters:
client- clientpath- the path to lock
-
InterProcessMutex
public InterProcessMutex(org.apache.curator.framework.CuratorFramework client, java.lang.String path, LockInternalsDriver driver)- Parameters:
client- clientpath- the path to lockdriver- lock driver
-
-
Method Detail
-
acquire
public void acquire() throws java.lang.ExceptionAcquire the mutex - blocking until it's available. Note: the same thread can call acquire re-entrantly. Each call to acquire must be balanced by a call torelease()- Specified by:
acquirein interfaceInterProcessLock- Throws:
java.lang.Exception- ZK errors, connection interruptions
-
acquire
public boolean acquire(long time, java.util.concurrent.TimeUnit unit) throws java.lang.ExceptionAcquire the mutex - blocks until it's available or the given time expires. Note: the same thread can call acquire re-entrantly. Each call to acquire that returns true must be balanced by a call torelease()- Specified by:
acquirein interfaceInterProcessLock- Parameters:
time- time to waitunit- time unit- Returns:
- true if the mutex was acquired, false if not
- Throws:
java.lang.Exception- ZK errors, connection interruptions
-
isAcquiredInThisProcess
public boolean isAcquiredInThisProcess()
Returns true if the mutex is acquired by a thread in this JVM- Specified by:
isAcquiredInThisProcessin interfaceInterProcessLock- Returns:
- true/false
-
release
public void release() throws java.lang.ExceptionPerform one release of the mutex if the calling thread is the same thread that acquired it. If the thread had made multiple calls to acquire, the mutex will still be held when this method returns.- Specified by:
releasein interfaceInterProcessLock- Throws:
java.lang.Exception- ZK errors, interruptions, current thread does not own the lock
-
getParticipantNodes
public java.util.Collection<java.lang.String> getParticipantNodes() throws java.lang.ExceptionReturn a sorted list of all current nodes participating in the lock- Returns:
- list of nodes
- Throws:
java.lang.Exception- ZK errors, interruptions, etc.
-
makeRevocable
public void makeRevocable(RevocationListener<InterProcessMutex> listener)
Description copied from interface:RevocableMake the lock revocable. Your listener will get called when another process/thread wants you to release the lock. Revocation is cooperative.- Specified by:
makeRevocablein interfaceRevocable<InterProcessMutex>- Parameters:
listener- the listener
-
makeRevocable
public void makeRevocable(RevocationListener<InterProcessMutex> listener, java.util.concurrent.Executor executor)
Description copied from interface:RevocableMake the lock revocable. Your listener will get called when another process/thread wants you to release the lock. Revocation is cooperative.- Specified by:
makeRevocablein interfaceRevocable<InterProcessMutex>- Parameters:
listener- the listenerexecutor- executor for the listener
-
isOwnedByCurrentThread
public boolean isOwnedByCurrentThread()
Returns true if the mutex is acquired by the calling thread- Returns:
- true/false
-
getLockNodeBytes
protected byte[] getLockNodeBytes()
-
getLockPath
protected java.lang.String getLockPath()
-
-