Class PessimisticLockFactory

java.lang.Object
org.axonframework.common.lock.PessimisticLockFactory
All Implemented Interfaces:
LockFactory

public class PessimisticLockFactory extends Object implements LockFactory
Implementation of a LockFactory that uses a pessimistic locking strategy. Calls to obtainLock(java.lang.String) will block until a lock could be obtained or back off limit is reached, based on the settings provided, by throwing an exception. The latter will cause the command to fail, but will allow the calling thread to be freed. If a lock is obtained by a thread, that thread has guaranteed unique access.

Each thread can hold the same lock multiple times. The lock will only be released for other threads when the lock has been released as many times as it was obtained.

This lock can be used to ensure thread safe access to a number of objects, such as Aggregates and Sagas.

Since:
1.3
Author:
Allard Buijze, Michael Bischoff, Henrique Sena
  • Constructor Details

    • PessimisticLockFactory

      protected PessimisticLockFactory(PessimisticLockFactory.Builder builder)
      Creates an instance of the lock factory using the given builder containing the configuration properties to use.
      Parameters:
      builder - The building containing the configuration properties to use
  • Method Details

    • builder

      public static PessimisticLockFactory.Builder builder()
      Creates a builder to construct an instance of this LockFactory.
      Returns:
      a builder allowing the definition of properties for this Lock Factory.
    • usingDefaults

      public static PessimisticLockFactory usingDefaults()
      Creates an instance using default values, as defined in the properties of the PessimisticLockFactory.Builder.
      Returns:
      a PessimisticLockFactory instance using sensible default values
    • obtainLock

      public Lock obtainLock(String identifier)
      Obtain a lock for a resource identified by the given identifier. This method will block until a lock was successfully obtained.

      Note: when an exception occurs during the locking process, the lock may or may not have been allocated.

      Specified by:
      obtainLock in interface LockFactory
      Parameters:
      identifier - the identifier of the lock to obtain.
      Returns:
      A handle to release the lock. If the thread that releases the lock does not hold the lock a IllegalMonitorStateException is thrown.
      Throws:
      IllegalArgumentException - Thrown when the given identifier is null.