Class JpaCustomizedResourceLockRepository
java.lang.Object
com.broadleafcommerce.common.messaging.provider.jpa.repository.JpaCustomizedResourceLockRepository
- All Implemented Interfaces:
JpaResourceLockRepository
,ResourceLockRepository<JpaResourceLock,
,UUID> org.springframework.data.repository.Repository<JpaResourceLock,
UUID>
public class JpaCustomizedResourceLockRepository
extends Object
implements JpaResourceLockRepository
Specialized implementation of
JpaResourceLockRepository
.- Author:
- Jeff Fischer, Chris Kittrell (ckittrell)
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionJpaCustomizedResourceLockRepository
(org.springframework.transaction.support.TransactionTemplate template, ResourceLockProperties resourceLockProperties) -
Method Summary
Modifier and TypeMethodDescription<V> V
doWithLock
(@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> V
doWithTemporaryLock
(@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> V
doWithTemporaryLock
(@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 String
getConceptKeyOrDefault
(String conceptKey) protected String
getSandboxIdOrDefault
(String sandboxId) protected List<JpaResourceLock>
identifyLocksToBePurged
(@NonNull Class<?> type, @NonNull Duration lockTtl, String startingId) Deprecated, for removal: This API element is subject to removal in a future version.protected List<JpaResourceLock>
identifyLocksToBePurged
(@NonNull Set<String> types, @NonNull Duration lockTtl, String startingId) Identifies locks that are eligible to be purgedlockResource
(@NonNull String contextId, @NonNull Class<?> type, String sandboxId, String conceptKey) Lock a resource indefinitely based on identifying information.lockResourceTemporarily
(@NonNull String contextId, @NonNull Class<?> type, String sandboxId, String conceptKey) Lock a resource temporarily based on identifying information.lockResourceTemporarily
(@NonNull String contextId, @NonNull Class<?> type, String sandboxId, String conceptKey, @NonNull Duration stagnationThreshold) Lock a resource temporarily based on identifying information.void
purgeLocks
(@NonNull Set<String> lockTypes, @NonNull Duration lockTtl) This table will be highly susceptible to deadlocks.protected void
releaseLock
(@NonNull JpaResourceLock lock) Deletes the provided lockReport on the status of the lock.void
unlockResource
(@NonNull String token) Unlock the resource so that other requesters can acquire the lock on the resource.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.broadleafcommerce.common.messaging.repository.ResourceLockRepository
purgeLocks
-
Field Details
-
LOCK_INSERT
- See Also:
-
LOCK_UPDATE
- See Also:
-
-
Constructor Details
-
JpaCustomizedResourceLockRepository
public JpaCustomizedResourceLockRepository(org.springframework.transaction.support.TransactionTemplate template, ResourceLockProperties resourceLockProperties)
-
-
Method Details
-
lockResource
public String lockResource(@NonNull @NonNull String contextId, @NonNull @NonNull Class<?> type, @Nullable String sandboxId, @Nullable String conceptKey) Description copied from interface:ResourceLockRepository
Lock 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, aResourceLockException
is thrown.- Specified by:
lockResource
in interfaceResourceLockRepository<JpaResourceLock,
UUID> - 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:ResourceLockRepository
Lock 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, aResourceLockException
is thrown.- Specified by:
lockResourceTemporarily
in interfaceResourceLockRepository<JpaResourceLock,
UUID> - 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:
-
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:ResourceLockRepository
Lock 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, aResourceLockException
is thrown.- Specified by:
lockResourceTemporarily
in interfaceResourceLockRepository<JpaResourceLock,
UUID> - 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
Description copied from interface:ResourceLockRepository
Unlock the resource so that other requesters can acquire the lock on the resource.- Specified by:
unlockResource
in interfaceResourceLockRepository<JpaResourceLock,
UUID> - Parameters:
token
- The identifying token for the lock
-
status
Description copied from interface:ResourceLockRepository
Report on the status of the lock. Especially useful if the token is distributed outside of a discrete flow.- Specified by:
status
in interfaceResourceLockRepository<JpaResourceLock,
UUID> - 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:ResourceLockRepository
A convenience method forResourceLockRepository.lockResource(String, Class, String, String)
andResourceLockRepository.unlockResource(String)
to obtainin a lock, perform an operation and then unlock- Specified by:
doWithLock
in interfaceResourceLockRepository<JpaResourceLock,
UUID> - 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:ResourceLockRepository
A convenience method forResourceLockRepository.lockResource(String, Class, String, String)
andResourceLockRepository.unlockResource(String)
to obtain a temporary lock, perform an operation and then unlock- Specified by:
doWithTemporaryLock
in interfaceResourceLockRepository<JpaResourceLock,
UUID> - 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:
-
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:ResourceLockRepository
A convenience method forResourceLockRepository.lockResource(String, Class, String, String)
andResourceLockRepository.unlockResource(String)
to obtainin a lock, perform an operation and then unlock- Specified by:
doWithTemporaryLock
in interfaceResourceLockRepository<JpaResourceLock,
UUID> - 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
This table will be highly susceptible to deadlocks. Therefore, we should execute small delete transactions in the same order that the locks were created should avoid these issues. Additionally, the failure to release a single lock should not stop us from releasing all other identified locks.- Specified by:
purgeLocks
in interfaceResourceLockRepository<JpaResourceLock,
UUID> - Parameters:
lockTypes
- The types of locks that are to be removedlockTtl
- How long the lock should be held. If the lock is too old, then it should be removed.
-
identifyLocksToBePurged
@Deprecated(forRemoval=true) protected List<JpaResourceLock> identifyLocksToBePurged(@NonNull @NonNull Class<?> type, @NonNull @NonNull Duration lockTtl, @Nullable String startingId) Deprecated, for removal: This API element is subject to removal in a future version.in favor ofidentifyLocksToBePurged(Set, Duration, String)
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
-
identifyLocksToBePurged
protected List<JpaResourceLock> identifyLocksToBePurged(@NonNull @NonNull Set<String> types, @NonNull @NonNull Duration lockTtl, @Nullable String startingId) Identifies locks that are eligible to be purged- Parameters:
types
- The types of locks 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
Deletes the provided lock- Parameters:
lock
- The lock that is to be released
-
getSandboxIdOrDefault
-
getConceptKeyOrDefault
-
identifyLocksToBePurged(Set, Duration, String)