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 TypeMethodDescriptionattemptTakeover
(String contextId, Class<?> type, String conceptKey, String completionState, Duration stagnationThreshold) Attempt to take over a lock that has not reached a completed state.<V> V
doWithLock
(@NonNull String contextId, @NonNull Class<?> type, String optional, 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 optional, 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 optional, 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 JpaResourceLock
protected JpaResourceLock
protected String
getOptionalOrDefault
(String optional) protected List<JpaResourceLock>
identifyLocksToBePurged
(Class<?> type, Duration lockTtl, String startingId) Deprecated, for removal: This API element is subject to removal in a future version.protected List<JpaResourceLock>
identifyLocksToBePurged
(Set<String> types, Duration lockTtl, String startingId) Identifies locks that are eligible to be purgedlockResource
(@NonNull String contextId, @NonNull Class<?> type, String optional, String conceptKey) Lock a resource indefinitely based on identifying information.lockResourceTemporarily
(@NonNull String contextId, @NonNull Class<?> type, String optional, String conceptKey) Lock a resource temporarily based on identifying information.lockResourceTemporarily
(@NonNull String contextId, @NonNull Class<?> type, String optional, 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
(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.boolean
updateState
(String token, String state) Update the state for the process associated with this lockMethods 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 optional, @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 contextIdoptional
- Optional information associated with the lock. Common values are sandboxId and status.conceptKey
- a generally descriptive key that identifies the overall concept for which the lock is taking place- Returns:
- The token that identifies the lock
-
attemptTakeover
public LockState attemptTakeover(String contextId, Class<?> type, @Nullable String conceptKey, String completionState, Duration stagnationThreshold) Description copied from interface:ResourceLockRepository
Attempt to take over a lock that has not reached a completed state. If successful, 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:
attemptTakeover
in interfaceResourceLockRepository<JpaResourceLock,
UUID> - Parameters:
contextId
- a unique id of the obtain a lock fortype
- grouping for the contextIdconceptKey
- a generally descriptive key that identifies the overall concept for which the lock is taking placecompletionState
- The status with which the process associated with the lock is considered complete. A lock without this status is a valid candidate for takeover.stagnationThreshold
- The amount of time to pass before the process associated with the lock is considered abandoned. A lock that has surpassed this stagnation threshold is a valid candidate for takeover.- Returns:
- The new token and last reported status information from the lock previously
-
updateState
Description copied from interface:ResourceLockRepository
Update the state for the process associated with this lock- Specified by:
updateState
in interfaceResourceLockRepository<JpaResourceLock,
UUID> - Parameters:
token
- The lock tokenstate
- The new state- Returns:
- Whether the update was successful
-
lockResourceTemporarily
public String lockResourceTemporarily(@NonNull @NonNull String contextId, @NonNull @NonNull Class<?> type, @Nullable String optional, @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 contextIdoptional
- Optional information associated with the lock. Common values are sandboxId and status.conceptKey
- 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 optional, @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 contextIdoptional
- Optional information associated with the lock. Common values are sandboxId and status.conceptKey
- 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 optional, @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 contextIdoptional
- Optional information associated with the lock. Common values are sandboxId and status.conceptKey
- 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 optional, @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 contextIdoptional
- Optional information associated with the lock. Common values are sandboxId and status.conceptKey
- 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 optional, @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 contextIdoptional
- Optional information associated with the lock. Common values are sandboxId and status.conceptKey
- 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(Class<?> type, 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(Set<String> types, 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
-
getLock
@Nullable protected JpaResourceLock getLock(String contextId, Class<?> type, @Nullable String optional, @Nullable String conceptKey) -
getLock
-
getOptionalOrDefault
-
getConceptKeyOrDefault
-
identifyLocksToBePurged(Set, Duration, String)