Class LockingObjectReference<T>
- java.lang.Object
-
- com.identityworksllc.iiq.common.threads.LockingObjectReference<T>
-
- Type Parameters:
T
- The type of the contained object
- All Implemented Interfaces:
AutoCloseable
public final class LockingObjectReference<T> extends Object implements AutoCloseable
An object reference wrapped by a ReentrantLock.This allows either directly invoking a function on the locked object or a more procedural lock/unlock pattern. This is fairer and safer than synchronizing on the object, since it can be interrupted (via Thread.interrupt(), e.g. on a task termination) and fairly chooses the longest-waiting thread to execute next.
In either usage, threads will wait forever for the lock. Threads will emit an INFO level log message every 30 seconds indicating that they are still waiting. Additionally, threads will emit a DEBUG message upon acquiring the lock. Both messages will have a UUID indicating a unique lock name, for tracing purposes.
If you are calling this from Beanshell, you can directly pass the name of a Beanshell method, along with the ‘this’ reference, such as:
objReference.lockAndAct(this, “beanshellMethodName”);
On lock acquisition, the specified Beanshell method in the ‘this’ scope will be invoked, passing the object as the only argument. The object will be automatically freed after the method completes.
You can also use this class via a try/finally structure, such as:
Object lockedObject = objReference.lock(); try { // do stuff here } finally { objReference.unlock(); }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
LockingObjectReference.LockedObjectAction<T>
The interface to be implemented by any locked object actions
-
Constructor Summary
Constructors Constructor Description LockingObjectReference(T object)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Unlocks the object, allowing use of this class in a try-with-resources contextvoid
lockAndAct(bsh.This beanshell, String methodName)
Locks the object, then passes it to the given Beanshell method.void
lockAndAct(LockingObjectReference.LockedObjectAction<T> action)
Locks the object, then passes it toLockingObjectReference.LockedObjectAction.accept(Object)
.T
lockObject()
Locks the object (waiting forever if necessary), then returns the objectvoid
unlockObject()
Unlocks the object
-
-
-
Constructor Detail
-
LockingObjectReference
public LockingObjectReference(T object)
-
-
Method Detail
-
close
public void close()
Unlocks the object, allowing use of this class in a try-with-resources context- Specified by:
close
in interfaceAutoCloseable
-
lockAndAct
public void lockAndAct(LockingObjectReference.LockedObjectAction<T> action) throws sailpoint.tools.GeneralException
Locks the object, then passes it toLockingObjectReference.LockedObjectAction.accept(Object)
.- Parameters:
action
- The function to execute against the object after it is locked- Throws:
sailpoint.tools.GeneralException
- if any failures occur
-
lockAndAct
public void lockAndAct(bsh.This beanshell, String methodName) throws sailpoint.tools.GeneralException
Locks the object, then passes it to the given Beanshell method.- Parameters:
beanshell
- The Beanshell context (‘this’ in a script)methodName
- The name of a Beanshell method in the current ‘this’ context or a parent- Throws:
sailpoint.tools.GeneralException
- if any failures occur
-
lockObject
public T lockObject() throws sailpoint.tools.GeneralException
Locks the object (waiting forever if necessary), then returns the object- Returns:
- The object, now exclusive to this thread
- Throws:
sailpoint.tools.GeneralException
- if any failures occur
-
unlockObject
public void unlockObject()
Unlocks the object
-
-