|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.jackrabbit.spi.commons.lock.Locked
public abstract class Locked
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.
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 | |
---|---|
static Object |
TIMED_OUT
Object returned when timeout is reached without being able to call run(javax.jcr.Node) while holding the lock. |
Constructor Summary | |
---|---|
Locked()
|
Method Summary | |
---|---|
protected abstract Object |
run(Node node)
This method is executed while holding the lock. |
Object |
with(Node lockable,
boolean isDeep)
Executes run(javax.jcr.Node) while the lock on lockable is held. |
Object |
with(Node lockable,
boolean isDeep,
long timeout)
Executes the method run(javax.jcr.Node) within the scope of a lock held on
lockable . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final Object TIMED_OUT
run(javax.jcr.Node)
while holding the lock.
Constructor Detail |
---|
public Locked()
Method Detail |
---|
public Object with(Node lockable, boolean isDeep) throws RepositoryException, InterruptedException
run(javax.jcr.Node)
while the lock on lockable
is held.
This method will block until run(javax.jcr.Node)
is executed while holding the
lock on node lockable
.
lockable
- a lockable node.isDeep
- true
if lockable
will be locked
deep.
run(javax.jcr.Node)
.
IllegalArgumentException
- if lockable
is not
mix:lockable.
RepositoryException
- if run(javax.jcr.Node)
throws an exception.
InterruptedException
- if this thread is interrupted while waiting
for the lock on node lockable
.public Object with(Node lockable, boolean isDeep, long timeout) throws UnsupportedRepositoryOperationException, RepositoryException, InterruptedException
run(javax.jcr.Node)
within the scope of a lock held on
lockable
.
lockable
- the node where the lock is obtained from.isDeep
- true
if lockable
will be locked
deep.timeout
- time in milliseconds to wait at most to aquire the lock.
run(javax.jcr.Node)
or TIMED_OUT
if the
lock on lockable
could not be aquired within the
specified timeout.
IllegalArgumentException
- if timeout
is negative or
lockable
is not
mix:lockable.
RepositoryException
- if run(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 node
lockable
.protected abstract Object run(Node node) throws RepositoryException
node
- The Node
on which the lock is placed.
with()
.
RepositoryException
- if an error occurs.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |