Class LockService
java.lang.Object
com.broadleafcommerce.dataexchange.service.LockService
-
Constructor Summary
ConstructorsConstructorDescriptionLockService
(DataExchangeLockCacheProperties properties) The methods ofLockService
are static, as is the LOCK_CACHE, which is aLRUMap
. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends ExchangeObject,
R>
RdoSynchronized
(T exchangeObject, String lockName, Function<T, R> function) Makes use of aLRUMap
to store mutex objects for synchronization.static <T,
R> R doSynchronized
(T mutex, Function<T, R> function) Uses the mutex to synchronize the execution of the provided function.static Object
getLock
(ExchangeObject exchangeObject, String key) To be used in places where synchronization is needed.static Object
To be used in places where synchronization is needed.static void
initialize
(DataExchangeLockCacheProperties properties) The methods ofLockService
are static, as is the LOCK_CACHE, which is aLRUMap
.static boolean
-
Constructor Details
-
LockService
The methods ofLockService
are static, as is the LOCK_CACHE, which is aLRUMap
. This constructor initializes static components if they are not already initialized.- Parameters:
properties
-
-
-
Method Details
-
initialize
The methods ofLockService
are static, as is the LOCK_CACHE, which is aLRUMap
. 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
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
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 objectkey
- the key to use for the lock- Returns:
- the lock object
- See Also:
-
doSynchronized
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 functionfunction
- 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 aLRUMap
to store mutex objects for synchronization. The benefit of this over thedoSynchronized(Object, Function)
method is that it allows you to synchronize on a more granular level. Rather than synchronize on theexchangeObject
, 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.
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 locklockName
- any arbitrary string value (e.g. "my-operation")function
- to execute synchronously- Returns:
- any value that the function returns, or null
- This uses a
-