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 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 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
      optional - 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 to ResourceLockRepository.unlockResource(String) when the work requiring the lock is complete. If unable to acquire the lock, a ResourceLockException is thrown.
      Specified by:
      attemptTakeover in interface ResourceLockRepository<JpaResourceLock,UUID>
      Parameters:
      contextId - a unique id of the obtain a lock for
      type - grouping for the contextId
      conceptKey - a generally descriptive key that identifies the overall concept for which the lock is taking place
      completionState - 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

      public boolean updateState(String token, String state)
      Description copied from interface: ResourceLockRepository
      Update the state for the process associated with this lock
      Specified by:
      updateState in interface ResourceLockRepository<JpaResourceLock,UUID>
      Parameters:
      token - The lock token
      state - 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 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
      optional - 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 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
      optional - 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
      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 optional, @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
      optional - 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
      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 optional, @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
      optional - 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
      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 optional, @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
      optional - 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
      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(Class<?> type, 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(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 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(JpaResourceLock lock)
      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

      @Nullable protected JpaResourceLock getLock(String token)
    • getOptionalOrDefault

      protected String getOptionalOrDefault(@Nullable String optional)
    • getConceptKeyOrDefault

      protected String getConceptKeyOrDefault(@Nullable String conceptKey)