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 ofJpaResourceLockRepository
.- Author:
- Jeff Fischer, Chris Kittrell (ckittrell)
-
-
Constructor Summary
Constructors Constructor Description JpaCustomizedResourceLockRepository(org.springframework.transaction.support.TransactionTemplate template, ResourceLockProperties resourceLockProperties)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <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)
Identifies locks that are eligible to be purgedString
lockResource(@NonNull String contextId, @NonNull Class<?> type, String sandboxId, String conceptKey)
Lock a resource indefinitely based on identifying information.String
lockResourceTemporarily(@NonNull String contextId, @NonNull Class<?> type, String sandboxId, String conceptKey)
Lock a resource temporarily based on identifying information.String
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 Class<?> type, @NonNull Duration lockTtl)
This table will be highly susceptible to deadlocks.protected void
releaseLock(@NonNull JpaResourceLock lock)
Deletes the provided lockLockStatus
status(String token)
Report 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.
-
-
-
Constructor Detail
-
JpaCustomizedResourceLockRepository
public JpaCustomizedResourceLockRepository(org.springframework.transaction.support.TransactionTemplate 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: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:
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: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
public void unlockResource(@NonNull @NonNull String token)
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
public LockStatus status(String token)
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:
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: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
public void purgeLocks(@NonNull @NonNull Class<?> type, @NonNull @NonNull Duration lockTtl)
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:
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<JpaResourceLock> identifyLocksToBePurged(@NonNull @NonNull Class<?> type, @NonNull @NonNull Duration lockTtl, @Nullable String 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(@NonNull @NonNull JpaResourceLock lock)
Deletes the provided lock- Parameters:
lock
- The lock that is to be released
-
-