Class DefaultLocalJvmLockService
- java.lang.Object
-
- com.broadleafcommerce.search.index.core.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 Summary
Constructors Constructor Description DefaultLocalJvmLockService()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanisDistributed()Indicates if this component is designed for use across JVMs.booleanisLocked(String lockName)Checks to see if a lock has already been acquired for the referenced object.Stringlock(String lockName)Attempts to create a lock based on the lock name.voidunlock(String lockName, String lockKey)Attempts to unlock the lock for the given lock name.
-
-
-
Method Detail
-
isDistributed
public final boolean isDistributed()
Description copied from interface:DistributableIndicates if this component is designed for use across JVMs.- Specified by:
isDistributedin interfaceDistributable- Returns:
- whether this component can be distributed across JVMs
-
isLocked
public boolean isLocked(@NonNull String lockName)Description copied from interface:LockServiceChecks to see if a lock has already been acquired for the referenced object.- Specified by:
isLockedin interfaceLockService- 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:LockServiceAttempts 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:
lockin interfaceLockService- 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 LockExceptionDescription copied from interface:LockServiceAttempts to unlock the lock for the given lock name.- Specified by:
unlockin interfaceLockService- Parameters:
lockName- the lock to unlocklockKey- the key for the lock- Throws:
LockException- if the lock could not be unlocked
-
-