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 Details

  • 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 to ResourceLockRepository.unlockResource(String) when the work requiring the lock is complete. If unable to acquire the lock, a ResourceLockException is thrown.
      Specified by:
      lockResource in interface ResourceLockRepository<JpaResourceLock,UUID>
      Parameters:
      contextId - a unique id of the obtain a lock for
      type - grouping for the contextId
      sandboxId - sandbox associated with the entity
      conceptKey - 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 to ResourceLockRepository.unlockResource(String) when the work requiring the lock is complete. If unable to acquire the lock, a ResourceLockException is thrown.
      Specified by:
      lockResourceTemporarily in interface ResourceLockRepository<JpaResourceLock,UUID>
      Parameters:
      contextId - a unique id of the obtain a lock for
      type - grouping for the contextId
      sandboxId - sandbox associated with the entity
      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 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 to ResourceLockRepository.unlockResource(String) when the work requiring the lock is complete. If unable to acquire the lock, a ResourceLockException is thrown.
      Specified by:
      lockResourceTemporarily in interface ResourceLockRepository<JpaResourceLock,UUID>
      Parameters:
      contextId - a unique id of the obtain a lock for
      type - grouping for the contextId
      sandboxId - sandbox associated with the entity
      conceptKey - a generally descriptive key that identifies the overall concept for which the lock is taking place
      stagnationThreshold - 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 interface ResourceLockRepository<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 interface ResourceLockRepository<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 for ResourceLockRepository.lockResource(String, Class, String, String) and ResourceLockRepository.unlockResource(String) to obtainin a lock, perform an operation and then unlock
      Specified by:
      doWithLock in interface ResourceLockRepository<JpaResourceLock,UUID>
      Type Parameters:
      V - type returned from operation
      Parameters:
      contextId - a unique id of the obtain a lock for
      type - grouping for the contextId
      sandboxId - sandbox associated with the entity
      conceptKey - a generally descriptive key that identifies the overall concept for which the lock is taking place
      operation - 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 for ResourceLockRepository.lockResource(String, Class, String, String) and ResourceLockRepository.unlockResource(String) to obtain a temporary lock, perform an operation and then unlock
      Specified by:
      doWithTemporaryLock in interface ResourceLockRepository<JpaResourceLock,UUID>
      Type Parameters:
      V - type returned from operation
      Parameters:
      contextId - a unique id of the obtain a lock for
      type - grouping for the contextId
      sandboxId - sandbox associated with the entity
      conceptKey - a generally descriptive key that identifies the overall concept for which the lock is taking place
      operation - 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 for ResourceLockRepository.lockResource(String, Class, String, String) and ResourceLockRepository.unlockResource(String) to obtainin a lock, perform an operation and then unlock
      Specified by:
      doWithTemporaryLock in interface ResourceLockRepository<JpaResourceLock,UUID>
      Type Parameters:
      V - type returned from operation
      Parameters:
      contextId - a unique id of the obtain a lock for
      type - grouping for the contextId
      sandboxId - sandbox associated with the entity
      conceptKey - a generally descriptive key that identifies the overall concept for which the lock is taking place
      stagnationThreshold - how long the lock should be held
      operation - the operation to perform within the lock
      Returns:
      the result of invoking operation
    • purgeLocks

      public void purgeLocks(@NonNull @NonNull Set<String> lockTypes, @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 interface ResourceLockRepository<JpaResourceLock,UUID>
      Parameters:
      lockTypes - The types of locks that are to be removed
      lockTtl - 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.
      Identifies locks that are eligible to be purged
      Parameters:
      type - The type of lock that should be identified
      lockTtl - 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 identified
      lockTtl - 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
    • getSandboxIdOrDefault

      protected String getSandboxIdOrDefault(@Nullable String sandboxId)
    • getConceptKeyOrDefault

      protected String getConceptKeyOrDefault(@Nullable String conceptKey)