Package com.sodius.oslc.server.trs.store
Class TrsStoreSynchronizer
java.lang.Object
com.sodius.oslc.server.trs.store.TrsStoreSynchronizer
Provides
synchronized (and non-synchronized) access to a
TrsStore. This class is to guarantee store methods are never executed concurrently but one at a time across the whole web application.
It also provides a ResourceEventStore implementation whose methods are non-synchronized,
so that callers (usually listeners in the application) do not need to take care on their own about this.
- Since:
- 1.10.0
-
Constructor Summary
ConstructorsConstructorDescriptionTrsStoreSynchronizer(TrsStore store) Creates a synchronizer instance wrapping the specified store. -
Method Summary
Modifier and TypeMethodDescription<R> RapplyLock(TrsStoreFunction<R> function) Executes a function using the underlying synchronizer, which guarantees only one function can be executed at a time across the whole web application.<R> RapplyUnlock(TrsStoreFunction<R> function) Executes a read-only function using the underlyingTrsStoreinstance.protected ExecutorServiceCreates the executor service for storing events in the store.Returns an event store designed for listeners registered in the application.booleanDetermines whether the event store has remaining event storage requests being processed asynchronously.
-
Constructor Details
-
TrsStoreSynchronizer
Creates a synchronizer instance wrapping the specified store. The constructor callsTrsStore.createSynchronizer()to obtain an Executor to execute operations sequentially.- Parameters:
store- a TRS store.
-
-
Method Details
-
applyLock
Executes a function using the underlying synchronizer, which guarantees only one function can be executed at a time across the whole web application. The function takes as argument the wrappedTrsStoreinstance.This method is to use when TRS data needs to be updated in the store. Prefer using
applyUnlock(TrsStoreFunction)for read-only operations.- Type Parameters:
R- the type of the result of the function- Parameters:
function- the function to execute through the synchronizer- Returns:
- the result of the specified function
- Throws:
TrsStoreException- when the function raises such exception or the synchronizer fails to complete the execution- Since:
- 3.5.0
-
applyUnlock
Executes a read-only function using the underlyingTrsStoreinstance. The function is executed right away, without any lock, and concurrently with other functions using this same method.This method is to use exclusively for reading TRS data from the store. Any attempt to modify the store will be rejected with an exception. Caller shall use
applyLock(TrsStoreFunction)for write operations.- Type Parameters:
R- the type of the result of the function- Parameters:
function- the function to execute on the store- Returns:
- the result of the specified function
- Throws:
TrsStoreException- when the function raises such exception- Since:
- 3.5.0
-
getResourceEventStore
Returns an event store designed for listeners registered in the application. Such listeners can callResourceEventStore.store(ResourceEvent...)methods without taking care on their own of the synchronization. This store implementation records in memory the events to be stored and immediately returns, so that listeners are guaranteed not to be blocked until the events are stored. The actual storage is performed in a separate thread and synchronized with the TrsStore using theapplyLock(TrsStoreFunction)method.- Returns:
- an asynchronized event store for application listeners.
-
isResourceEventStoreIdle
public boolean isResourceEventStoreIdle()Determines whether the event store has remaining event storage requests being processed asynchronously.- Returns:
trueif no event storage request is currently being processed,falseif the store is actively storing data.
-
createResourceEventExecutor
Creates the executor service for storing events in the store. Default implementation usesExecutors.newSingleThreadExecutor()passingThreads.getThreadFactory()as argument. Applications may override this method to customize theExecutorService.- Returns:
- the executor service for storing events in the store.
- Since:
- 3.9.0
-