Package org.copperengine.core.lockmgr
Interface LockManager
-
- All Known Subinterfaces:
PersistentLockManager
- All Known Implementing Classes:
PersistentLockManagerImpl
,TransientLockManager
public interface LockManager
A service to obtain/manage locks, e.g. to functionally synchronize workflow instances. Currently, two implementations exist for the LockManager. - One of them is for persistent engines and synchronizes workflow instances through a database lock which thus also works on distributed engines operating on a single data base (Only one workflow at a time obtains the lock with the symbolic lockId) - The second implementation is provided to be used in transient engines. There is no synchronization possibility for distributed transient engines and thus, only one workflow at a time obtains the lock on a single engine. If you run multiple engines, it can happen that multiple workflows (each on a different engine) operate concurrently on the same lock (i.e same symbolic lockId)!
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description java.lang.String
acquireLock(java.lang.String lockId, java.lang.String workflowInstanceId)
Acquires a lock with the specified id.void
releaseLock(java.lang.String lockId, java.lang.String workflowInstanceId)
Releases the specified lock.
-
-
-
Method Detail
-
acquireLock
java.lang.String acquireLock(java.lang.String lockId, java.lang.String workflowInstanceId)
Acquires a lock with the specified id. If the lock is free, then the lock is assigned to the caller and the methods returnnull
.Otherwise, if the lock is currently held by another entity, then the method returns a correlationId that the caller has to use to wait for. As soon as this lock is assigned to the caller, the lock manager creates a
Response
for this specified correlationId, containing theLockResult
.Example:
private void acquireLock(final String lockId) throws Interrupt { for (;;) { logger.info("Going to acquire lock '{}'", lockId); final String cid = lockManager.acquireLock(lockId, this.getId()); if (cid == null) { logger.info("Successfully acquired lock '{}'", lockId); return; } else { logger.info("Lock '{}' is currently not free - calling wait...", lockId); wait(WaitMode.ALL, 10000, cid); final Response<LockResult> result = getAndRemoveResponse(cid); logger.info("lock result={}", result); if (result.isTimeout()) { logger.info("Failed to acquire lock: Timeout - trying again..."); } else if (result.getResponse() != LockResult.OK) { logger.error("Failed to acquire lock: {} - trying again...", result.getResponse()); } else { logger.info("Successfully acquired lock '{}'", lockId); return; } } } }
- Parameters:
lockId
- symbolic lock idworkflowInstanceId
- requestor/owner of this lock- Returns:
null
if the lock was free and was assigned to the caller (workflowInstanceId) otherwise a correlationId to wait for.- Throws:
java.lang.RuntimeException
- in case of technical problems
-
releaseLock
void releaseLock(java.lang.String lockId, java.lang.String workflowInstanceId)
Releases the specified lock. If the workflow with the specified workflowId is not yet the owner of the lock (i.e. it is still waiting to retrieve the lock), the acquireLock request is removed from the queue.- Parameters:
lockId
- symbolic lock idworkflowInstanceId
- requestor/owner of this lock- Throws:
java.lang.RuntimeException
- in case of technical problems
-
-