Search in sources :

Example 1 with Locked

use of org.apache.jackrabbit.util.Locked in project jackrabbit by apache.

the class LockTest method testSequence.

/**
     * Tests the utility {@link org.apache.jackrabbit.util.Locked} by
     * implementing a persistent counter.
     */
public void testSequence() throws RepositoryException {
    final Node counter = testRootNode.addNode(nodeName1);
    counter.setProperty("value", 0);
    counter.addMixin(mixLockable);
    superuser.save();
    final List<Thread> worker = new ArrayList<Thread>();
    for (int i = 0; i < NUM_THREADS; i++) {
        worker.add(new Thread() {

            private final int threadNumber = worker.size();

            public void run() {
                final Session s;
                try {
                    s = getHelper().getSuperuserSession();
                } catch (RepositoryException e) {
                    fail(e.getMessage());
                    return;
                }
                try {
                    for (int i = 0; i < NUM_VALUE_GETS; i++) {
                        Node n = (Node) s.getItem(counter.getPath());
                        long currentValue = ((Long) new Locked() {

                            protected Object run(Node n) throws RepositoryException {
                                Property seqProp = n.getProperty("value");
                                long value = seqProp.getLong();
                                seqProp.setValue(++value);
                                s.save();
                                return new Long(value);
                            }
                        }.with(n, false)).longValue();
                        log.println("Thread" + threadNumber + ": got sequence number: " + currentValue);
                        // do a random wait
                        Thread.sleep(new Random().nextInt(100));
                    }
                } catch (RepositoryException e) {
                    log.println("exception while running code with lock:" + e.getMessage());
                } catch (InterruptedException e) {
                    log.println(Thread.currentThread() + " interrupted while waiting for lock");
                } finally {
                    s.logout();
                }
            }
        });
    }
    for (Iterator<Thread> it = worker.iterator(); it.hasNext(); ) {
        it.next().start();
    }
    for (Iterator<Thread> it = worker.iterator(); it.hasNext(); ) {
        try {
            it.next().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Also used : Node(javax.jcr.Node) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Locked(org.apache.jackrabbit.util.Locked) Random(java.util.Random) Property(javax.jcr.Property) Session(javax.jcr.Session)

Example 2 with Locked

use of org.apache.jackrabbit.util.Locked in project jackrabbit by apache.

the class LockTest method testSequenceWithTimeout.

/**
     * Tests the utility {@link org.apache.jackrabbit.util.Locked} by
     * implementing a persistent counter with a timeout when the next value of
     * the counter is retrieved. The number of values that can be retrieved by
     * this test depends on system performance and the configured persistence
     * manager.
     */
public void testSequenceWithTimeout() throws RepositoryException {
    final Node counter = testRootNode.addNode(nodeName1);
    counter.setProperty("value", 0);
    counter.addMixin(mixLockable);
    superuser.save();
    final List<Thread> worker = new ArrayList<Thread>();
    for (int i = 0; i < NUM_THREADS; i++) {
        worker.add(new Thread() {

            private final int threadNumber = worker.size();

            public void run() {
                final Session s;
                try {
                    s = getHelper().getSuperuserSession();
                } catch (RepositoryException e) {
                    fail(e.getMessage());
                    return;
                }
                try {
                    for (int i = 0; i < NUM_VALUE_GETS; i++) {
                        Node n = (Node) s.getItem(counter.getPath());
                        Object ret = new Locked() {

                            protected Object run(Node n) throws RepositoryException {
                                Property seqProp = n.getProperty("value");
                                long value = seqProp.getLong();
                                seqProp.setValue(++value);
                                s.save();
                                return new Long(value);
                            }
                        }.with(n, false, // expect a value after
                        10 * 1000);
                        // ten seconds
                        if (ret == Locked.TIMED_OUT) {
                            log.println("Thread" + threadNumber + ": could not get a sequence number within 10 seconds");
                        } else {
                            long currentValue = ((Long) ret).longValue();
                            log.println("Thread" + threadNumber + ": got sequence number: " + currentValue);
                        }
                        // do a random wait
                        Thread.sleep(new Random().nextInt(100));
                    }
                } catch (RepositoryException e) {
                    log.println("exception while running code with lock:" + e.getMessage());
                } catch (InterruptedException e) {
                    log.println(Thread.currentThread() + " interrupted while waiting for lock");
                } finally {
                    s.logout();
                }
            }
        });
    }
    for (Iterator<Thread> it = worker.iterator(); it.hasNext(); ) {
        it.next().start();
    }
    for (Iterator<Thread> it = worker.iterator(); it.hasNext(); ) {
        try {
            it.next().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Also used : Node(javax.jcr.Node) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Locked(org.apache.jackrabbit.util.Locked) Random(java.util.Random) Property(javax.jcr.Property) Session(javax.jcr.Session)

Example 3 with Locked

use of org.apache.jackrabbit.util.Locked in project jackrabbit by apache.

the class LockTest method testLockUtility.

/**
     * Tests the utility {@link org.apache.jackrabbit.util.Locked} by
     * implementing running multiple threads concurrently that apply changes to
     * a lockable node.
     */
public void testLockUtility() throws RepositoryException {
    final Node lockable = testRootNode.addNode(nodeName1);
    lockable.addMixin(mixLockable);
    superuser.save();
    final List<Thread> worker = new ArrayList<Thread>();
    for (int i = 0; i < NUM_THREADS; i++) {
        worker.add(new Thread() {

            private final int threadNumber = worker.size();

            public void run() {
                final Session s;
                try {
                    s = getHelper().getSuperuserSession();
                } catch (RepositoryException e) {
                    fail(e.getMessage());
                    return;
                }
                try {
                    for (int i = 0; i < NUM_CHANGES; i++) {
                        Node n = (Node) s.getItem(lockable.getPath());
                        new Locked() {

                            protected Object run(Node n) throws RepositoryException {
                                String nodeName = "node" + threadNumber;
                                if (n.hasNode(nodeName)) {
                                    n.getNode(nodeName).remove();
                                } else {
                                    n.addNode(nodeName);
                                }
                                s.save();
                                log.println("Thread" + threadNumber + ": saved modification");
                                return null;
                            }
                        }.with(n, false);
                        // do a random wait
                        Thread.sleep(new Random().nextInt(100));
                    }
                } catch (RepositoryException e) {
                    log.println("exception while running code with lock:" + e.getMessage());
                } catch (InterruptedException e) {
                    log.println(Thread.currentThread() + " interrupted while waiting for lock");
                } finally {
                    s.logout();
                }
            }
        });
    }
    for (Iterator<Thread> it = worker.iterator(); it.hasNext(); ) {
        it.next().start();
    }
    for (Iterator<Thread> it = worker.iterator(); it.hasNext(); ) {
        try {
            it.next().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Also used : Locked(org.apache.jackrabbit.util.Locked) Random(java.util.Random) Node(javax.jcr.Node) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

Aggregations

ArrayList (java.util.ArrayList)3 Random (java.util.Random)3 Node (javax.jcr.Node)3 RepositoryException (javax.jcr.RepositoryException)3 Session (javax.jcr.Session)3 Locked (org.apache.jackrabbit.util.Locked)3 Property (javax.jcr.Property)2