Class DefaultLocalJvmLockService

  • All Implemented Interfaces:
    Distributable, LockService

    public class DefaultLocalJvmLockService
    extends Object
    implements LockService
    This component allows for the tracking of a lock reference for a single process ID or reference.

    By definition, this implementation is non-distributed and only works in a single JVM. Callers should acquire a lock by calling the lock method, and then, in a finally block, call the unlock method:

     String lockName = CatalogIndexableType.PRODUCT.getName();
     boolean obtained = false;
     String key = null;
     if (! lockService.isLocked(lockName)) {
       try {
         key = lockService.lock(lockName);
         obtained = true;
         ... //Do work...
       
       } catch (LockException e) {
         //Log the error and return or retry since this lock is already in use.
       } finally {
         if (obtained) {
           try {
             lockService.unlock(lockName, key);
           } catch (LockException e) {
             //Log the error. //This should not happen unless the underlying key/reference relationship has been removed....
           }
         }
       }
     }
     
    Author:
    Kelly Tisdell (ktisdell)
    • Constructor Detail

      • DefaultLocalJvmLockService

        public DefaultLocalJvmLockService()
    • Method Detail

      • isDistributed

        public final boolean isDistributed()
        Description copied from interface: Distributable
        Indicates if this component is designed for use across JVMs.
        Specified by:
        isDistributed in interface Distributable
        Returns:
        whether this component can be distributed across JVMs
      • isLocked

        public boolean isLocked​(@NonNull
                                String lockName)
        Description copied from interface: LockService
        Checks to see if a lock has already been acquired for the referenced object.
        Specified by:
        isLocked in interface LockService
        Parameters:
        lockName - the lock to check
        Returns:
        whether the lock has already been acquired
      • lock

        public String lock​(@NonNull
                           String lockName)
                    throws LockException
        Description copied from interface: LockService
        Attempts to create a lock based on the lock name. This does not necessarily synchronize or otherwise block threads. It just creates a lock reference for the for the specified lock name. Additional attempts to create a lock with the same name will fail until a call is made to the unlock method to unlock the lock for the specific lock name.
        Specified by:
        lock in interface LockService
        Parameters:
        lockName - the lock to create
        Returns:
        the key for the created lock
        Throws:
        LockException - if a lock could not be obtained
      • unlock

        public void unlock​(@NonNull
                           String lockName,
                           @NonNull
                           String lockKey)
                    throws LockException
        Description copied from interface: LockService
        Attempts to unlock the lock for the given lock name.
        Specified by:
        unlock in interface LockService
        Parameters:
        lockName - the lock to unlock
        lockKey - the key for the lock
        Throws:
        LockException - if the lock could not be unlocked