org.apache.curator.framework.recipes.locks
Class InterProcessMutex

java.lang.Object
  extended by org.apache.curator.framework.recipes.locks.InterProcessMutex
All Implemented Interfaces:
InterProcessLock, Revocable<InterProcessMutex>

public class InterProcessMutex
extends 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
InterProcessMutex(CuratorFramework client, String path)
           
 
Method Summary
 void acquire()
          Acquire the mutex - blocking until it's available.
 boolean acquire(long time, TimeUnit unit)
          Acquire the mutex - blocks until it's available or the given time expires.
protected  byte[] getLockNodeBytes()
           
 Collection<String> getParticipantNodes()
          Return a sorted list of all current nodes participating in the lock
 boolean isAcquiredInThisProcess()
          Returns true if the mutex is acquired by a thread in this JVM
 void makeRevocable(RevocationListener<InterProcessMutex> listener)
          Make the lock revocable.
 void makeRevocable(RevocationListener<InterProcessMutex> listener, Executor executor)
          Make the lock revocable.
 void release()
          Perform one release of the mutex if the calling thread is the same thread that acquired it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InterProcessMutex

public InterProcessMutex(CuratorFramework client,
                         String path)
Parameters:
client - client
path - the path to lock
Method Detail

acquire

public void acquire()
             throws Exception
Acquire 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 to release()

Specified by:
acquire in interface InterProcessLock
Throws:
Exception - ZK errors, connection interruptions

acquire

public boolean acquire(long time,
                       TimeUnit unit)
                throws Exception
Acquire 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 to release()

Specified by:
acquire in interface InterProcessLock
Parameters:
time - time to wait
unit - time unit
Returns:
true if the mutex was acquired, false if not
Throws:
Exception - ZK errors, connection interruptions

isAcquiredInThisProcess

public boolean isAcquiredInThisProcess()
Returns true if the mutex is acquired by a thread in this JVM

Specified by:
isAcquiredInThisProcess in interface InterProcessLock
Returns:
true/false

release

public void release()
             throws Exception
Perform 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:
release in interface InterProcessLock
Throws:
Exception - ZK errors, interruptions, current thread does not own the lock

getParticipantNodes

public Collection<String> getParticipantNodes()
                                       throws Exception
Return a sorted list of all current nodes participating in the lock

Returns:
list of nodes
Throws:
Exception - ZK errors, interruptions, etc.

makeRevocable

public void makeRevocable(RevocationListener<InterProcessMutex> listener)
Description copied from interface: Revocable
Make the lock revocable. Your listener will get called when another process/thread wants you to release the lock. Revocation is cooperative.

Specified by:
makeRevocable in interface Revocable<InterProcessMutex>
Parameters:
listener - the listener

makeRevocable

public void makeRevocable(RevocationListener<InterProcessMutex> listener,
                          Executor executor)
Description copied from interface: Revocable
Make the lock revocable. Your listener will get called when another process/thread wants you to release the lock. Revocation is cooperative.

Specified by:
makeRevocable in interface Revocable<InterProcessMutex>
Parameters:
listener - the listener
executor - executor for the listener

getLockNodeBytes

protected byte[] getLockNodeBytes()


Copyright © 2011–2014 The Apache Software Foundation. All rights reserved.