Class CuratorLockHelper


  • public class CuratorLockHelper
    extends Object
    Helper class for creating and managing Curator locks, and converting timeouts and exceptions thrown by Curator into LockAcquisitionExceptions.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void acquire​(org.apache.curator.framework.recipes.locks.InterProcessLock lock, long time, TimeUnit unit)
      Tries to acquire the specified lock, waiting up to the specified timeout period.
      org.apache.curator.framework.recipes.locks.InterProcessMutex createInterProcessMutex​(org.apache.curator.framework.CuratorFramework client, String lockPath)
      Creates a Curator lock instance for the given path.
      void releaseLockQuietlyIfHeld​(org.apache.curator.framework.recipes.locks.InterProcessLock lock)
      Release the given lock quietly (ignoring exceptions) if the current process holds it.
      void releaseQuietly​(org.apache.curator.framework.recipes.locks.InterProcessLock lock)
      Release the given lock, ignoring if null or if any exception occurs releasing the lock.
    • Constructor Detail

      • CuratorLockHelper

        public CuratorLockHelper()
    • Method Detail

      • createInterProcessMutex

        public org.apache.curator.framework.recipes.locks.InterProcessMutex createInterProcessMutex​(org.apache.curator.framework.CuratorFramework client,
                                                                                                    String lockPath)
        Creates a Curator lock instance for the given path.
        Parameters:
        client - Curator client
        lockPath - the ZooKeeper base lock path
        Returns:
        a Curator lock instance (InterProcessMutex)
      • acquire

        public void acquire​(org.apache.curator.framework.recipes.locks.InterProcessLock lock,
                            long time,
                            TimeUnit unit)
        Tries to acquire the specified lock, waiting up to the specified timeout period. If Curator throws any exception, or if the timeout expires, the appropriate exception is thrown.
        Parameters:
        lock - the lock to acquire
        time - the timeout quantity
        unit - the timeout unit
        Throws:
        LockAcquisitionFailureException - if the lock throws any exception during acquisition
        LockAcquisitionTimeoutException - if the lock acquisition times out
      • releaseQuietly

        public void releaseQuietly​(org.apache.curator.framework.recipes.locks.InterProcessLock lock)
        Release the given lock, ignoring if null or if any exception occurs releasing the lock.
        Parameters:
        lock - to release
      • releaseLockQuietlyIfHeld

        public void releaseLockQuietlyIfHeld​(org.apache.curator.framework.recipes.locks.InterProcessLock lock)
        Release the given lock quietly (ignoring exceptions) if the current process holds it.
        Parameters:
        lock - to release, if held