Search in sources :

Example 11 with ConcurrentAccessTimeoutException

use of javax.ejb.ConcurrentAccessTimeoutException in project tomee by apache.

the class StatefulConcurrencyTest method testConcurrentMethodCall.

public void testConcurrentMethodCall() throws Exception {
    MyLocalBeanImpl.semaphore = new Semaphore(0);
    final InitialContext ctx = new InitialContext();
    final MyLocalBean bean = (MyLocalBean) ctx.lookup("MyLocalBeanImplLocal");
    final MyLocalBean bean2 = (MyLocalBean) ctx.lookup("MyLocalBeanImplLocal");
    final CallRentrantThread call = new CallRentrantThread(bean, 3000);
    (new Thread(call)).start();
    // ensure the call on thread came in
    assertTrue(MyLocalBeanImpl.semaphore.tryAcquire(1, 30, TimeUnit.SECONDS));
    try {
        java.util.logging.Logger.getLogger(this.getClass().getName()).info("Expecting a SEVERE javax.ejb.ConcurrentAccessTimeoutException");
        bean2.callRentrant(bean, 0);
        fail("Expected exception");
    } catch (final Exception e) {
        if (e.getCause() instanceof ConcurrentAccessTimeoutException) {
        // that's what we want
        } else {
            throw e;
        }
    }
}
Also used : Semaphore(java.util.concurrent.Semaphore) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) InitialContext(javax.naming.InitialContext) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException)

Example 12 with ConcurrentAccessTimeoutException

use of javax.ejb.ConcurrentAccessTimeoutException in project tomee by apache.

the class StatelessInstanceManagerPoolingTest method testStatelessBeanTimeout.

public void testStatelessBeanTimeout() throws Exception {
    final InitialContext ctx = new InitialContext();
    final Object object = ctx.lookup("CounterBeanLocal");
    assertTrue("instanceof counter", object instanceof Counter);
    final CountDownLatch timeouts = new CountDownLatch(10);
    final CountDownLatch startPistol = new CountDownLatch(1);
    final CountDownLatch startingLine = new CountDownLatch(10);
    final Counter counter = (Counter) object;
    // Do a business method...
    final Runnable r = new Runnable() {

        public void run() {
            try {
                counter.race(startingLine, startPistol);
            } catch (final ConcurrentAccessTimeoutException ex) {
                comment("Leap Start");
                timeouts.countDown();
            }
        }
    };
    comment("On your mark!");
    for (int i = 0; i < 20; i++) {
        final Thread t = new Thread(r);
        t.start();
    }
    // Wait for the beans to reach the start line
    assertTrue("expected 10 invocations", startingLine.await(3000, TimeUnit.MILLISECONDS));
    comment("Get Set!");
    // Wait for the other beans timeout
    assertTrue("expected 10 timeouts", timeouts.await(3000, TimeUnit.MILLISECONDS));
    assertEquals(10, instances.get(), 1.1);
    comment("Go!");
    // go
    startPistol.countDown();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) InitialContext(javax.naming.InitialContext)

Example 13 with ConcurrentAccessTimeoutException

use of javax.ejb.ConcurrentAccessTimeoutException in project tomee by apache.

the class SingletonContainer method aquireLock.

private Lock aquireLock(final boolean read, final Duration accessTimeout, final Instance instance, final Method runMethod) {
    final Lock lock;
    if (read) {
        lock = instance.lock.readLock();
    } else {
        lock = instance.lock.writeLock();
    }
    final boolean lockAcquired;
    if (accessTimeout == null || accessTimeout.getTime() < 0) {
        // wait indefinitely for a lock
        // noinspection LockAcquiredButNotSafelyReleased
        lock.lock();
        lockAcquired = true;
    } else if (accessTimeout.getTime() == 0) {
        // concurrent calls are not allowed, lock only once
        lockAcquired = lock.tryLock();
    } else {
        // try to get a lock within the specified period.
        try {
            lockAcquired = lock.tryLock(accessTimeout.getTime(), accessTimeout.getUnit());
        } catch (final InterruptedException e) {
            throw (ConcurrentAccessTimeoutException) new ConcurrentAccessTimeoutException("Unable to get " + (read ? "read" : "write") + " lock within specified time on '" + runMethod.getName() + "' method for: " + instance.bean.getClass().getName()).initCause(e);
        }
    }
    // Did we acquire the lock to the current execution?
    if (!lockAcquired) {
        throw new ConcurrentAccessTimeoutException("Unable to get " + (read ? "read" : "write") + " lock on '" + runMethod.getName() + "' method for: " + instance.bean.getClass().getName());
    }
    return lock;
}
Also used : ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) Lock(java.util.concurrent.locks.Lock)

Example 14 with ConcurrentAccessTimeoutException

use of javax.ejb.ConcurrentAccessTimeoutException in project tomee by apache.

the class StatelessInstanceManager method getInstance.

/**
 * Removes an instance from the pool and returns it for use
 * by the container in business methods.
 *
 * If the pool is at it's limit the StrictPooling flag will
 * cause this thread to wait.
 *
 * If StrictPooling is not enabled this method will create a
 * new stateless bean instance performing all required injection
 * and callbacks before returning it in a method ready state.
 *
 * @param callContext ThreadContext
 * @return Object
 * @throws OpenEJBException
 */
public Instance getInstance(final ThreadContext callContext) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final Data data = (Data) beanContext.getContainerData();
    Instance instance = null;
    try {
        final Pool<Instance>.Entry entry = data.poolPop();
        if (entry != null) {
            instance = entry.get();
            instance.setPoolEntry(entry);
        }
    } catch (final TimeoutException e) {
        final String msg = "No instances available in Stateless Session Bean pool.  Waited " + data.accessTimeout.toString();
        final ConcurrentAccessTimeoutException timeoutException = new ConcurrentAccessTimeoutException(msg);
        timeoutException.fillInStackTrace();
        throw new ApplicationException(timeoutException);
    } catch (final InterruptedException e) {
        Thread.interrupted();
        throw new OpenEJBException("Unexpected Interruption of current thread: ", e);
    }
    if (null == instance) {
        instance = createInstance(callContext, beanContext);
    }
    return instance;
}
Also used : BeanContext(org.apache.openejb.BeanContext) OpenEJBException(org.apache.openejb.OpenEJBException) ApplicationException(org.apache.openejb.ApplicationException) InterceptorInstance(org.apache.openejb.core.interceptor.InterceptorInstance) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) Pool(org.apache.openejb.util.Pool) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) TimeoutException(java.util.concurrent.TimeoutException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException)

Example 15 with ConcurrentAccessTimeoutException

use of javax.ejb.ConcurrentAccessTimeoutException in project tomee by apache.

the class MdbInstanceManager method getInstance.

/**
 * Removes an instance from the pool and returns it for use
 * by the container in business methods.
 *
 * If the pool is at it's limit the StrictPooling flag will
 * cause this thread to wait.
 *
 * If StrictPooling is not enabled this method will create a
 * new bean instance performing all required injection
 * and callbacks before returning it in a method ready state.
 *
 * @param callContext ThreadContext
 * @return Object
 * @throws OpenEJBException
 */
public Instance getInstance(final ThreadContext callContext) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final Data data = (Data) beanContext.getContainerData();
    Instance instance = null;
    try {
        final Pool<Instance>.Entry entry = data.poolPop();
        if (entry != null) {
            instance = entry.get();
            instance.setPoolEntry(entry);
        }
    } catch (final TimeoutException e) {
        final String msg = "No instances available in Message Driven Bean pool.  Waited " + data.getAccessTimeout().toString();
        final ConcurrentAccessTimeoutException timeoutException = new ConcurrentAccessTimeoutException(msg);
        timeoutException.fillInStackTrace();
        throw new ApplicationException(timeoutException);
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new OpenEJBException("Unexpected Interruption of current thread: ", e);
    }
    if (null == instance) {
        instance = createInstance(beanContext);
    }
    return instance;
}
Also used : BeanContext(org.apache.openejb.BeanContext) OpenEJBException(org.apache.openejb.OpenEJBException) ApplicationException(org.apache.openejb.ApplicationException) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) Pool(org.apache.openejb.util.Pool) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) TimeoutException(java.util.concurrent.TimeoutException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException)

Aggregations

ConcurrentAccessTimeoutException (javax.ejb.ConcurrentAccessTimeoutException)17 CountDownLatch (java.util.concurrent.CountDownLatch)7 Test (org.junit.Test)6 ExecutorService (java.util.concurrent.ExecutorService)5 RemoteException (java.rmi.RemoteException)3 TimeoutException (java.util.concurrent.TimeoutException)3 Lock (java.util.concurrent.locks.Lock)3 ConcurrentAccessException (javax.ejb.ConcurrentAccessException)3 ApplicationException (org.apache.openejb.ApplicationException)3 OpenEJBException (org.apache.openejb.OpenEJBException)3 ArrayList (java.util.ArrayList)2 InitialContext (javax.naming.InitialContext)2 BeanContext (org.apache.openejb.BeanContext)2 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)2 Pool (org.apache.openejb.util.Pool)2 ComponentInstance (org.jboss.as.ee.component.ComponentInstance)2 EJBComponent (org.jboss.as.ejb3.component.EJBComponent)2 FencedLock (com.hazelcast.cp.lock.FencedLock)1 InvocationInfo (com.sun.ejb.InvocationInfo)1 MethodLockInfo (com.sun.ejb.MethodLockInfo)1