Class MongoCustomizedResourceLockRepository
- java.lang.Object
-
- com.broadleafcommerce.common.messaging.provider.mongo.repository.MongoCustomizedResourceLockRepository
-
- All Implemented Interfaces:
MongoResourceLockRepository,ResourceLockRepository<MongoResourceLock,org.bson.types.ObjectId>,org.springframework.data.repository.Repository<MongoResourceLock,org.bson.types.ObjectId>
public class MongoCustomizedResourceLockRepository extends Object implements MongoResourceLockRepository
Specialized implementation ofMongoResourceLockRepository.- Author:
- Jeff Fischer, Chris Kittrell (ckittrell)
-
-
Constructor Summary
Constructors Constructor Description MongoCustomizedResourceLockRepository(org.springframework.data.mongodb.core.MongoTemplate template, ResourceLockProperties resourceLockProperties)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <V> VdoWithLock(@NonNull String contextId, @NonNull Class<?> type, String sandboxId, String conceptKey, @NonNull Supplier<V> operation)A convenience method forResourceLockRepository.lockResource(String, Class, String, String)andResourceLockRepository.unlockResource(String)to obtainin a lock, perform an operation and then unlock<V> VdoWithTemporaryLock(@NonNull String contextId, @NonNull Class<?> type, String sandboxId, String conceptKey, @NonNull Supplier<V> operation)A convenience method forResourceLockRepository.lockResource(String, Class, String, String)andResourceLockRepository.unlockResource(String)to obtain a temporary lock, perform an operation and then unlock<V> VdoWithTemporaryLock(@NonNull String contextId, @NonNull Class<?> type, String sandboxId, String conceptKey, Duration stagnationThreshold, @NonNull Supplier<V> operation)A convenience method forResourceLockRepository.lockResource(String, Class, String, String)andResourceLockRepository.unlockResource(String)to obtainin a lock, perform an operation and then unlockprotected StringgetConceptKeyOrDefault(String conceptKey)protected StringgetSandboxIdOrDefault(String sandboxId)protected List<MongoResourceLock>identifyLocksToBePurged(@NonNull Class<?> type, @NonNull Duration lockTtl, org.bson.types.ObjectId startingId)Identifies locks that are eligible to be purgedStringlockResource(@NonNull String contextId, @NonNull Class<?> type, String sandboxId, String conceptKey)Lock a resource indefinitely based on identifying information.StringlockResourceTemporarily(@NonNull String contextId, @NonNull Class<?> type, String sandboxId, String conceptKey)Lock a resource temporarily based on identifying information.StringlockResourceTemporarily(@NonNull String contextId, @NonNull Class<?> type, String sandboxId, String conceptKey, @NonNull Duration stagnationThreshold)Lock a resource temporarily based on identifying information.voidpurgeLocks(@NonNull Class<?> type, @NonNull Duration lockTtl)Deletes all non-expiring locks that are out of date according to thelockTtland haveJpaResourceLock.getTypeAlias()equal to the providedtype.protected voidreleaseLock(MongoResourceLock lock)Deletes the provided lock, if its lock timestamp has not been updated since the lock was identifiedLockStatusstatus(String token)Report on the status of the lock.voidunlockResource(@NonNull String token)Unlock the resource so that other requesters can acquire the lock on the resource.
-
-
-
Constructor Detail
-
MongoCustomizedResourceLockRepository
public MongoCustomizedResourceLockRepository(org.springframework.data.mongodb.core.MongoTemplate template, ResourceLockProperties resourceLockProperties)
-
-
Method Detail
-
lockResource
public String lockResource(@NonNull @NonNull String contextId, @NonNull @NonNull Class<?> type, @Nullable String sandboxId, @Nullable String conceptKey)
Description copied from interface:ResourceLockRepositoryLock a resource indefinitely based on identifying information. Will return a token representing the lock that is suitable to be passed toResourceLockRepository.unlockResource(String)when the work requiring the lock is complete. If unable to acquire the lock, aResourceLockExceptionis thrown.- Specified by:
lockResourcein interfaceResourceLockRepository<MongoResourceLock,org.bson.types.ObjectId>- Parameters:
contextId- a unique id of the obtain a lock fortype- grouping for the contextIdsandboxId- sandbox associated with the entityconceptKey- a generally descriptive key that identifies the overall concept for which the lock is taking place- Returns:
- The token that identifies the lock
-
lockResourceTemporarily
public String lockResourceTemporarily(@NonNull @NonNull String contextId, @NonNull @NonNull Class<?> type, @Nullable String sandboxId, @Nullable String conceptKey)
Description copied from interface:ResourceLockRepositoryLock a resource temporarily based on identifying information. Will return a token representing the lock that is suitable to be passed toResourceLockRepository.unlockResource(String)when the work requiring the lock is complete. If unable to acquire the lock, aResourceLockExceptionis thrown.- Specified by:
lockResourceTemporarilyin interfaceResourceLockRepository<MongoResourceLock,org.bson.types.ObjectId>- Parameters:
contextId- a unique id of the obtain a lock fortype- grouping for the contextIdsandboxId- sandbox associated with the entityconceptKey- a generally descriptive key that identifies the overall concept for which the lock is taking place- Returns:
- The token that identifies the lock
- See Also:
ResourceLockProperties.getDefaultStagnationThreshold()
-
lockResourceTemporarily
public String lockResourceTemporarily(@NonNull @NonNull String contextId, @NonNull @NonNull Class<?> type, @Nullable String sandboxId, @Nullable String conceptKey, @NonNull @NonNull Duration stagnationThreshold)
Description copied from interface:ResourceLockRepositoryLock a resource temporarily based on identifying information. Will return a token representing the lock that is suitable to be passed toResourceLockRepository.unlockResource(String)when the work requiring the lock is complete. If unable to acquire the lock, aResourceLockExceptionis thrown.- Specified by:
lockResourceTemporarilyin interfaceResourceLockRepository<MongoResourceLock,org.bson.types.ObjectId>- Parameters:
contextId- a unique id of the obtain a lock fortype- grouping for the contextIdsandboxId- sandbox associated with the entityconceptKey- a generally descriptive key that identifies the overall concept for which the lock is taking placestagnationThreshold- how long the lock should be held- Returns:
- The token that identifies the lock
-
unlockResource
public void unlockResource(@NonNull @NonNull String token)Description copied from interface:ResourceLockRepositoryUnlock the resource so that other requesters can acquire the lock on the resource.- Specified by:
unlockResourcein interfaceResourceLockRepository<MongoResourceLock,org.bson.types.ObjectId>- Parameters:
token- The identifying token for the lock
-
status
public LockStatus status(String token)
Description copied from interface:ResourceLockRepositoryReport on the status of the lock. Especially useful if the token is distributed outside of a discrete flow.- Specified by:
statusin interfaceResourceLockRepository<MongoResourceLock,org.bson.types.ObjectId>- Parameters:
token- The identifying token for the lock- Returns:
- The status of the lock
-
doWithLock
public <V> V doWithLock(@NonNull @NonNull String contextId, @NonNull @NonNull Class<?> type, @Nullable String sandboxId, @Nullable String conceptKey, @NonNull @NonNull Supplier<V> operation)Description copied from interface:ResourceLockRepositoryA convenience method forResourceLockRepository.lockResource(String, Class, String, String)andResourceLockRepository.unlockResource(String)to obtainin a lock, perform an operation and then unlock- Specified by:
doWithLockin interfaceResourceLockRepository<MongoResourceLock,org.bson.types.ObjectId>- Type Parameters:
V- type returned fromoperation- Parameters:
contextId- a unique id of the obtain a lock fortype- grouping for the contextIdsandboxId- sandbox associated with the entityconceptKey- a generally descriptive key that identifies the overall concept for which the lock is taking placeoperation- the operation to perform within the lock- Returns:
- the result of invoking
operation
-
doWithTemporaryLock
public <V> V doWithTemporaryLock(@NonNull @NonNull String contextId, @NonNull @NonNull Class<?> type, @Nullable String sandboxId, @Nullable String conceptKey, @NonNull @NonNull Supplier<V> operation)Description copied from interface:ResourceLockRepositoryA convenience method forResourceLockRepository.lockResource(String, Class, String, String)andResourceLockRepository.unlockResource(String)to obtain a temporary lock, perform an operation and then unlock- Specified by:
doWithTemporaryLockin interfaceResourceLockRepository<MongoResourceLock,org.bson.types.ObjectId>- Type Parameters:
V- type returned fromoperation- Parameters:
contextId- a unique id of the obtain a lock fortype- grouping for the contextIdsandboxId- sandbox associated with the entityconceptKey- a generally descriptive key that identifies the overall concept for which the lock is taking placeoperation- the operation to perform within the lock- Returns:
- the result of invoking
operation - See Also:
ResourceLockProperties.getDefaultStagnationThreshold()
-
doWithTemporaryLock
public <V> V doWithTemporaryLock(@NonNull @NonNull String contextId, @NonNull @NonNull Class<?> type, @Nullable String sandboxId, @Nullable String conceptKey, @Nullable Duration stagnationThreshold, @NonNull @NonNull Supplier<V> operation)Description copied from interface:ResourceLockRepositoryA convenience method forResourceLockRepository.lockResource(String, Class, String, String)andResourceLockRepository.unlockResource(String)to obtainin a lock, perform an operation and then unlock- Specified by:
doWithTemporaryLockin interfaceResourceLockRepository<MongoResourceLock,org.bson.types.ObjectId>- Type Parameters:
V- type returned fromoperation- Parameters:
contextId- a unique id of the obtain a lock fortype- grouping for the contextIdsandboxId- sandbox associated with the entityconceptKey- a generally descriptive key that identifies the overall concept for which the lock is taking placestagnationThreshold- how long the lock should be heldoperation- the operation to perform within the lock- Returns:
- the result of invoking
operation
-
purgeLocks
public void purgeLocks(@NonNull @NonNull Class<?> type, @NonNull @NonNull Duration lockTtl)Description copied from interface:ResourceLockRepositoryDeletes all non-expiring locks that are out of date according to thelockTtland haveJpaResourceLock.getTypeAlias()equal to the providedtype.- Specified by:
purgeLocksin interfaceResourceLockRepository<MongoResourceLock,org.bson.types.ObjectId>- Parameters:
type- The type of lock that is to be removedlockTtl- How long the lock should be held. If the lock is too old, then it should be removed.
-
identifyLocksToBePurged
protected List<MongoResourceLock> identifyLocksToBePurged(@NonNull @NonNull Class<?> type, @NonNull @NonNull Duration lockTtl, @Nullable org.bson.types.ObjectId startingId)
Identifies locks that are eligible to be purged- Parameters:
type- The type of lock that should be identifiedlockTtl- How long the lock should be held. If the lock is too old, then it should be removed.startingId- The id from which to start the retrieval of a batch of candidate records.- Returns:
- The list of locks that are eligible to be purged
-
releaseLock
protected void releaseLock(MongoResourceLock lock)
Deletes the provided lock, if its lock timestamp has not been updated since the lock was identified- Parameters:
lock- The lock that is to be released
-
-