Class Locker

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class Locker
    extends java.lang.Object
    implements java.lang.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 Detail

      • Locker

        public Locker​(InterProcessLock lock,
                      long timeout,
                      java.util.concurrent.TimeUnit unit)
               throws java.lang.Exception
        Parameters:
        lock - a lock implementation (e.g. InterProcessMutex, InterProcessSemaphoreV2, etc.)
        timeout - max timeout to acquire lock
        unit - time unit of timeout
        Throws:
        java.lang.Exception - Curator errors or TimeoutException if the lock cannot be acquired within the timeout
    • Method Detail

      • close

        public void close()
                   throws java.lang.Exception
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.lang.Exception
      • releaseLock

        protected void releaseLock()
                            throws java.lang.Exception
        Throws:
        java.lang.Exception
      • acquireLock

        protected void acquireLock​(InterProcessLock lock)
                            throws java.lang.Exception
        Throws:
        java.lang.Exception
      • acquireLock

        protected boolean acquireLock​(InterProcessLock lock,
                                      long timeout,
                                      java.util.concurrent.TimeUnit unit)
                               throws java.lang.Exception
        Throws:
        java.lang.Exception