Class MemoryLock

java.lang.Object
org.pipservices3.components.lock.Lock
org.pipservices3.components.lock.MemoryLock
All Implemented Interfaces:
org.pipservices3.commons.config.IConfigurable, org.pipservices3.commons.config.IReconfigurable, ILock

public class MemoryLock extends Lock
Lock that is used to synchronize execution within one process using shared memory. Remember: This implementation is not suitable for synchronization of distributed processes. ### Configuration parameters ###
    options:
  • - retry_timeout: timeout in milliseconds to retry lock acquisition. (Default: 100)
### Example ###
 
 MemoryLock lock = new MemoryLock();
 try {
  // Processing...
 }
 finally  {
 lock.releaseLock("123", "key1");
  // Continue...
 });
 
See Also:
  • Constructor Details

    • MemoryLock

      public MemoryLock()
  • Method Details

    • tryAcquireLock

      public boolean tryAcquireLock(String correlationId, String key, int ttl)
      Makes a single attempt to acquire a lock by its key. It returns immediately a positive or negative result.
      Specified by:
      tryAcquireLock in interface ILock
      Specified by:
      tryAcquireLock in class Lock
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      key - a unique lock key to acquire.
      ttl - a lock timeout (time to live) in milliseconds.
      Returns:
      true if the lock was acquired and false otherwise.
    • releaseLock

      public void releaseLock(String correlationId, String key)
      Releases the lock with the given key.
      Specified by:
      releaseLock in interface ILock
      Specified by:
      releaseLock in class Lock
      Parameters:
      correlationId - (optional) transaction id to trace execution through call chain.
      key - a unique lock key to release.