Class Locked
- java.lang.Object
-
- org.apache.jackrabbit.spi.commons.lock.Locked
-
public abstract class Locked extends java.lang.Object
Deprecated.Use org.apache.jackrabbit.util.Locked instead.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 anInvalidItemStateException
. 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 atimeout
you need to check the return value whether therun
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 Modifier and Type Field Description static java.lang.Object
TIMED_OUT
Deprecated.Object returned when timeout is reached without being able to callrun(javax.jcr.Node)
while holding the lock.
-
Constructor Summary
Constructors Constructor Description Locked()
Deprecated.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description java.lang.Object
with(Node lockable, boolean isDeep)
Deprecated.Executesrun(javax.jcr.Node)
while the lock onlockable
is held.java.lang.Object
with(Node lockable, boolean isDeep, long timeout)
Deprecated.Executes the methodrun(javax.jcr.Node)
within the scope of a lock held onlockable
.
-
-
-
Field Detail
-
TIMED_OUT
public static final java.lang.Object TIMED_OUT
Deprecated.Object returned when timeout is reached without being able to callrun(javax.jcr.Node)
while holding the lock.
-
-
Method Detail
-
with
public java.lang.Object with(Node lockable, boolean isDeep) throws RepositoryException, java.lang.InterruptedException
Deprecated.Executesrun(javax.jcr.Node)
while the lock onlockable
is held. This method will block untilrun(javax.jcr.Node)
is executed while holding the lock on nodelockable
.- Parameters:
lockable
- a lockable node.isDeep
-true
iflockable
will be locked deep.- Returns:
- the object returned by
run(javax.jcr.Node)
. - Throws:
java.lang.IllegalArgumentException
- iflockable
is not mix:lockable.RepositoryException
- ifrun(javax.jcr.Node)
throws an exception.java.lang.InterruptedException
- if this thread is interrupted while waiting for the lock on nodelockable
.
-
with
public java.lang.Object with(Node lockable, boolean isDeep, long timeout) throws UnsupportedRepositoryOperationException, RepositoryException, java.lang.InterruptedException
Deprecated.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
-true
iflockable
will be locked deep.timeout
- time in milliseconds to wait at most to aquire the lock.- Returns:
- the object returned by
run(javax.jcr.Node)
orTIMED_OUT
if the lock onlockable
could not be aquired within the specified timeout. - Throws:
java.lang.IllegalArgumentException
- iftimeout
is negative orlockable
is not mix:lockable.RepositoryException
- ifrun(javax.jcr.Node)
throws an exception.UnsupportedRepositoryOperationException
- if this repository does not support locking.java.lang.InterruptedException
- if this thread is interrupted while waiting for the lock on nodelockable
.
-
-