Class TrsStoreSynchronizer


  • public class TrsStoreSynchronizer
    extends Object
    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 Detail

      • TrsStoreSynchronizer

        public TrsStoreSynchronizer​(TrsStore store)
        Creates a synchronizer instance wrapping the specified store. The constructor calls TrsStore.createSynchronizer() to obtain an Executor to execute operations sequentially.
        Parameters:
        store - a TRS store.
    • Method Detail

      • applyLock

        public <R> R applyLock​(TrsStoreFunction<R> function)
                        throws TrsStoreException
        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 wrapped TrsStore instance.

        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

        public <R> R applyUnlock​(TrsStoreFunction<R> function)
                          throws TrsStoreException
        Executes a read-only function using the underlying TrsStore instance. 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

        public ResourceEventStore getResourceEventStore()
        Returns an event store designed for listeners registered in the application. Such listeners can call ResourceEventStore.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 the applyLock(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:
        true if no event storage request is currently being processed, false if the store is actively storing data.