Class LockService

java.lang.Object
com.broadleafcommerce.dataexchange.service.LockService

public class LockService extends Object
  • Constructor Details

    • LockService

      public LockService(DataExchangeLockCacheProperties properties)
      The methods of LockService are static, as is the LOCK_CACHE, which is a LRUMap. This constructor initializes static components if they are not already initialized.
      Parameters:
      properties -
  • Method Details

    • initialize

      public static void initialize(DataExchangeLockCacheProperties properties)
      The methods of LockService are static, as is the LOCK_CACHE, which is a LRUMap. This method initializes static components if they are not already initialized. If this has already been initialized, it will log a message and return.
      Parameters:
      properties -
    • isInitialized

      public static boolean isInitialized()
    • getLock

      public static Object getLock(String lockKey)
      To be used in places where synchronization is needed. This allows thread safe synchronization with string keys. Example: String lockKey = getLock(exchangeObject.getCorrelationId() + "-myService"); synchronized(lockKey) { // do something }
      Parameters:
      lockKey - the key to use for the lock
      Returns:
      the lock object
      See Also:
    • getLock

      public static Object getLock(@NonNull ExchangeObject exchangeObject, String key)
      To be used in places where synchronization is needed. This allows thread safe synchronization with string keys. This uses the correlationId from the exchange object and the key provided. Example: synchronized(product, "product-assets") { // do something }
      Parameters:
      exchangeObject - the exchange object
      key - the key to use for the lock
      Returns:
      the lock object
      See Also:
    • doSynchronized

      public static <T, R> R doSynchronized(@NonNull T mutex, @NonNull Function<T,R> function)
      Uses the mutex to synchronize the execution of the provided function. The mutex is passed to the function. The value returned by the function is returned by this method. This is typically used to set a Collection or Map on Entity where the Map or Collection might be null: LockService.doSynchronized(product, p -> { if (p.getPrices() == null) { p.setPrices(Collections.synchronizedList(new ArrayList<>())); } return null; }); This is often faster or more efficient than other methods in this service, which have to access a Cache or Map for mutex to synchronize on. The Cache needs to synchronize as well. Note that you should use care with this method as it can hurt performance if the function is doing any I/O or heavy processing or is otherwise slow to execute. This method is really meant for extremely fast functions such as the one, above.
      Type Parameters:
      T -
      R -
      Parameters:
      mutex - an object to synchronize on, and that will be passed to the function
      function - to execute synchronously
      Returns:
      any value that the function returns, or null
    • doSynchronized

      public static <T extends ExchangeObject, R> R doSynchronized(@NonNull T exchangeObject, String lockName, @NonNull Function<T,R> function)
      Makes use of a LRUMap to store mutex objects for synchronization. The benefit of this over the doSynchronized(Object, Function) method is that it allows you to synchronize on a more granular level. Rather than synchronize on the exchangeObject, for example, you can synchronize on the exchangeObject and an arbitrary name (e.g. "my-operation"). The drawbacks of this method are as follows:
      • This uses a LRUMap as a cache to store mutex object. Accessing the cache and then synchronizing is usually slower than just synchronizing on the mutex, especially when all that's happening is the instantiation of a Collection or Map, which is usually very fast.
      • Because this uses a shared LRUMap, the map needs enough capacity to hold all of the lock objects for the duration of a batch. The only way that items are removed from the cache is when the cache is full and new items are added.
      This method is here for convenience, but doSynchronized(Object, Function) is generally preferred.
      Type Parameters:
      T -
      R -
      Parameters:
      exchangeObject - any exchange object to be passed to the function; also used to create a lock
      lockName - any arbitrary string value (e.g. "my-operation")
      function - to execute synchronously
      Returns:
      any value that the function returns, or null