Search in sources :

Example 1 with Locked

use of org.apache.deltaspike.core.api.lock.Locked in project deltaspike by apache.

the class LockSupplierStorage method getLockSupplier.

protected LockSupplier getLockSupplier(final InvocationContext ic) {
    final Method key = ic.getMethod();
    LockSupplier operation = lockSuppliers.get(key);
    if (operation == null) {
        final Class declaringClass = key.getDeclaringClass();
        final AnnotatedType<Object> annotatedType = beanManager.createAnnotatedType(declaringClass);
        final AnnotatedMethod<?> annotatedMethod = AnnotatedMethods.findMethod(annotatedType, key);
        Locked config = annotatedMethod.getAnnotation(Locked.class);
        if (config == null) {
            config = annotatedType.getAnnotation(Locked.class);
        }
        final Locked.LockFactory factory = config.factory() != Locked.LockFactory.class ? Locked.LockFactory.class.cast(beanManager.getReference(beanManager.resolve(beanManager.getBeans(config.factory())), Locked.LockFactory.class, null)) : this;
        final ReadWriteLock writeLock = factory.newLock(annotatedMethod, config.fair());
        final long timeout = config.timeoutUnit().toMillis(config.timeout());
        final Lock lock = config.operation() == READ ? writeLock.readLock() : writeLock.writeLock();
        if (timeout > 0) {
            operation = new LockSupplier() {

                @Override
                public Lock get() {
                    try {
                        if (!lock.tryLock(timeout, TimeUnit.MILLISECONDS)) {
                            throw new IllegalStateException("Can't lock for " + key + " in " + timeout + "ms");
                        }
                    } catch (final InterruptedException e) {
                        Thread.interrupted();
                        throw new IllegalStateException("Locking interrupted", e);
                    }
                    return lock;
                }
            };
        } else {
            operation = new LockSupplier() {

                @Override
                public Lock get() {
                    lock.lock();
                    return lock;
                }
            };
        }
        final LockSupplier existing = lockSuppliers.putIfAbsent(key, operation);
        if (existing != null) {
            operation = existing;
        }
    }
    return operation;
}
Also used : Method(java.lang.reflect.Method) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Locked(org.apache.deltaspike.core.api.lock.Locked) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Aggregations

Method (java.lang.reflect.Method)1 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)1 Locked (org.apache.deltaspike.core.api.lock.Locked)1