Package org.apache.jackrabbit.util
Class Locked
java.lang.Object
org.apache.jackrabbit.util.Locked
- Direct Known Subclasses:
LockedWrapper
Locked is a utility to synchronize modifications on a lockable
node. The modification is applied while the lock on the node is held, thus
ensuring that the modification will never fail with an InvalidItemStateException. This utility can be used with any
JCR Repository, not just Jackrabbit.
The following example shows how this utility can be used to implement a persistent counter:
Node counter = ...;
long nextValue = ((Long) new Locked() {
protected Object run(Node counter) throws RepositoryException {
Property seqProp = counter.getProperty("value");
long value = seqProp.getLong();
seqProp.setValue(++value);
seqProp.save();
return new Long(value);
}
}.with(counter, false)).longValue();
If you specify a timeout you need to check the return value
whether the run method could be executed within the timeout
period:
Node counter = ...;
Object ret = new Locked() {
protected Object run(Node counter) throws RepositoryException {
Property seqProp = counter.getProperty("value");
long value = seqProp.getLong();
seqProp.setValue(++value);
seqProp.save();
return new Long(value);
}
}.with(counter, false);
if (ret == Locked.TIMED_OUT) {
// do whatever you think is appropriate in this case
} else {
// get the value
long nextValue = ((Long) ret).longValue();
}
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionExecutesrun(javax.jcr.Node)while the lock onlockableis held.Executesrun(javax.jcr.Node)while the lock onlockableis held.Executes the methodrun(javax.jcr.Node)within the scope of a lock held onlockable.Executes the methodrun(javax.jcr.Node)within the scope of a lock held onlockable.
-
Field Details
-
TIMED_OUT
Object returned when timeout is reached without being able to callrun(javax.jcr.Node)while holding the lock.
-
-
Constructor Details
-
Locked
public Locked()
-
-
Method Details
-
with
Executesrun(javax.jcr.Node)while the lock onlockableis held. This method will block untilrun(javax.jcr.Node)is executed while holding the lock on nodelockable.- Parameters:
lockable- a lockable node.isDeep-trueiflockablewill be locked deep.- Returns:
- the object returned by
run(javax.jcr.Node). - Throws:
IllegalArgumentException- iflockableis not mix:lockable.RepositoryException- ifrun(javax.jcr.Node)throws an exception.InterruptedException- if this thread is interrupted while waiting for the lock on nodelockable.
-
with
public Object with(Node lockable, boolean isDeep, boolean isSessionScoped) throws RepositoryException, InterruptedException Executesrun(javax.jcr.Node)while the lock onlockableis held. This method will block untilrun(javax.jcr.Node)is executed while holding the lock on nodelockable.- Parameters:
lockable- a lockable node.isDeep-trueiflockablewill be locked deep.isSessionScoped-trueif the lock is session scoped.- Returns:
- the object returned by
run(javax.jcr.Node). - Throws:
IllegalArgumentException- iflockableis not mix:lockable.RepositoryException- ifrun(javax.jcr.Node)throws an exception.InterruptedException- if this thread is interrupted while waiting for the lock on nodelockable.
-
with
public Object with(Node lockable, boolean isDeep, long timeout) throws UnsupportedRepositoryOperationException, RepositoryException, InterruptedException Executes the methodrun(javax.jcr.Node)within the scope of a lock held onlockable.- Parameters:
lockable- the node where the lock is obtained from.isDeep-trueiflockablewill be locked deep.timeout- time in milliseconds to wait at most to acquire the lock.- Returns:
- the object returned by
run(javax.jcr.Node)orTIMED_OUTif the lock onlockablecould not be acquired within the specified timeout. - Throws:
IllegalArgumentException- iftimeoutis negative orlockableis not mix:lockable.RepositoryException- ifrun(javax.jcr.Node)throws an exception.UnsupportedRepositoryOperationException- if this repository does not support locking.InterruptedException- if this thread is interrupted while waiting for the lock on nodelockable.
-
with
public Object with(Node lockable, boolean isDeep, long timeout, boolean isSessionScoped) throws UnsupportedRepositoryOperationException, RepositoryException, InterruptedException Executes the methodrun(javax.jcr.Node)within the scope of a lock held onlockable.- Parameters:
lockable- the node where the lock is obtained from.isDeep-trueiflockablewill be locked deep.timeout- time in milliseconds to wait at most to acquire the lock.isSessionScoped-trueif the lock is session scoped.- Returns:
- the object returned by
run(javax.jcr.Node)orTIMED_OUTif the lock onlockablecould not be acquired within the specified timeout. - Throws:
IllegalArgumentException- iftimeoutis negative orlockableis not mix:lockable.RepositoryException- ifrun(javax.jcr.Node)throws an exception.UnsupportedRepositoryOperationException- if this repository does not support locking.InterruptedException- if this thread is interrupted while waiting for the lock on nodelockable.
-