org.axonframework.common.lock
Class IdentifierBasedLock

java.lang.Object
  extended by org.axonframework.common.lock.IdentifierBasedLock

public class IdentifierBasedLock
extends Object

Locking mechanism that allows multiple threads to hold a lock, as long as the identifier of the lock they hold is not equal. When released, locks entries are automatically cleaned up.

The lock is re-entrant, meaning 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

Constructor Summary
IdentifierBasedLock()
          Creates a new IdentifierBasedLock instance.
 
Method Summary
 boolean hasLock(String identifier)
          Indicates whether the current thread hold a lock for the given identifier.
 void obtainLock(String identifier)
          Obtain a lock on the given identifier.
 void releaseLock(String identifier)
          Release the lock held on the given identifier.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IdentifierBasedLock

public IdentifierBasedLock()
Creates a new IdentifierBasedLock instance.

Deadlocks are detected across instances of the IdentifierBasedLock.

Method Detail

hasLock

public boolean hasLock(String identifier)
Indicates whether the current thread hold a lock for the given identifier.

Parameters:
identifier - The identifier of the lock to verify
Returns:
true if the current thread holds a lock, otherwise false

obtainLock

public void obtainLock(String identifier)
Obtain a lock on 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.

Parameters:
identifier - the identifier of the lock to obtain.

releaseLock

public void releaseLock(String identifier)
Release the lock held on the given identifier. If no valid lock is held by the current thread, an exception is thrown.

Parameters:
identifier - the identifier to release the lock for.
Throws:
IllegalStateException - if no lock was ever obtained for this aggregate
IllegalMonitorStateException - if a lock was obtained, but is not currently held by the current thread


Copyright © 2010-2016. All Rights Reserved.