Search in sources :

Example 1 with ConcurrentAccessException

use of javax.ejb.ConcurrentAccessException in project wildfly by wildfly.

the class SessionObjectReferenceTestCase method testEcho.

@Test
public void testEcho() throws Exception {
    String result = performCall("simple", "Hello+world");
    XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8)));
    List<String> results = (List<String>) decoder.readObject();
    List<Exception> exceptions = (List<Exception>) decoder.readObject();
    String sharedContext = (String) decoder.readObject();
    decoder.close();
    assertEquals(1, results.size());
    assertEquals("Echo Hello world", results.get(0));
    assertEquals(1, exceptions.size());
    assertTrue(exceptions.get(0) instanceof ConcurrentAccessException);
    assertEquals("Shared context", sharedContext);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XMLDecoder(java.beans.XMLDecoder) List(java.util.List) ConcurrentAccessException(javax.ejb.ConcurrentAccessException) TimeoutException(java.util.concurrent.TimeoutException) ConcurrentAccessException(javax.ejb.ConcurrentAccessException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with ConcurrentAccessException

use of javax.ejb.ConcurrentAccessException in project wildfly by wildfly.

the class NonPooledEJBComponentInstanceAssociatingInterceptor method processInvocation.

@Override
public Object processInvocation(InterceptorContext context) throws Exception {
    final EJBComponent component = getComponent(context, EJBComponent.class);
    // create the instance
    final ComponentInstance componentInstance = component.createInstance();
    context.putPrivateData(ComponentInstance.class, componentInstance);
    // if this is set to true we do not invoke instance.destroy
    // as we are not allowed to invoke pre-destroy callbacks
    boolean discard = false;
    try {
        return context.proceed();
    } catch (Exception ex) {
        final EJBComponent ejbComponent = component;
        // Detect app exception
        if (ejbComponent.getApplicationException(ex.getClass(), context.getMethod()) != null) {
            // it's an application exception, just throw it back.
            throw ex;
        }
        if (ex instanceof ConcurrentAccessTimeoutException || ex instanceof ConcurrentAccessException) {
            throw ex;
        }
        if (ex instanceof RuntimeException || ex instanceof RemoteException) {
            discard = true;
        }
        throw ex;
    } catch (final Error e) {
        discard = true;
        throw e;
    } catch (final Throwable t) {
        discard = true;
        throw new RuntimeException(t);
    } finally {
        // destroy the instance
        if (!discard) {
            componentInstance.destroy();
        }
    }
}
Also used : ComponentInstance(org.jboss.as.ee.component.ComponentInstance) ConcurrentAccessException(javax.ejb.ConcurrentAccessException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) RemoteException(java.rmi.RemoteException) EJBComponent(org.jboss.as.ejb3.component.EJBComponent) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) ConcurrentAccessException(javax.ejb.ConcurrentAccessException) RemoteException(java.rmi.RemoteException)

Example 3 with ConcurrentAccessException

use of javax.ejb.ConcurrentAccessException in project wildfly by wildfly.

the class PooledInstanceInterceptor method processInvocation.

@Override
public Object processInvocation(InterceptorContext context) throws Exception {
    PooledComponent<ComponentInstance> component = (PooledComponent<ComponentInstance>) getComponent(context, EJBComponent.class);
    ComponentInstance instance = component.getPool().get();
    context.putPrivateData(ComponentInstance.class, instance);
    boolean discarded = false;
    try {
        return context.proceed();
    } catch (Exception ex) {
        final EJBComponent ejbComponent = (EJBComponent) component;
        // Detect app exception
        if (ejbComponent.getApplicationException(ex.getClass(), context.getMethod()) != null) {
            // it's an application exception, just throw it back.
            throw ex;
        }
        if (ex instanceof ConcurrentAccessTimeoutException || ex instanceof ConcurrentAccessException) {
            throw ex;
        }
        if (ex instanceof RuntimeException || ex instanceof RemoteException) {
            discarded = true;
            component.getPool().discard(instance);
        }
        throw ex;
    } catch (final Error e) {
        discarded = true;
        component.getPool().discard(instance);
        throw e;
    } catch (final Throwable t) {
        discarded = true;
        component.getPool().discard(instance);
        throw new RuntimeException(t);
    } finally {
        if (!discarded) {
            component.getPool().release(instance);
        }
    }
}
Also used : ComponentInstance(org.jboss.as.ee.component.ComponentInstance) ConcurrentAccessException(javax.ejb.ConcurrentAccessException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) RemoteException(java.rmi.RemoteException) EJBComponent(org.jboss.as.ejb3.component.EJBComponent) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) ConcurrentAccessException(javax.ejb.ConcurrentAccessException) RemoteException(java.rmi.RemoteException)

Example 4 with ConcurrentAccessException

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

the class MultithreadTest method testStatelessBeanTimeout.

@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
@Test
public void testStatelessBeanTimeout() throws Exception {
    final CountDownLatch timeouts = new CountDownLatch(10);
    startPistol = new CountDownLatch(1);
    startingLine = new CountDownLatch(10);
    // Do a business method...
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    final Runnable r = new Runnable() {

        @Override
        public void run() {
            try {
                counter.race();
            } catch (ConcurrentAccessException ex) {
                comment("Leap Start");
                timeouts.countDown();
                assertEquals("No instances available in Stateless Session Bean pool.  Waited 100 MILLISECONDS", ex.getMessage());
            } catch (Throwable t) {
                error.set(t);
                // useless in another thread
                fail("Unexpected exception" + t.getClass().getName() + " " + t.getMessage());
            }
        }
    };
    comment("On your mark!");
    final Collection<Thread> threads = new ArrayList<>(20);
    for (int i = 0; i < 20; i++) {
        final Thread t = new Thread(r);
        threads.add(t);
        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(300000, TimeUnit.MILLISECONDS));
    if (error.get() != null) {
        error.get().printStackTrace();
        fail(error.get().getMessage());
    }
    assertEquals(10, CounterBean.instances.get());
    comment("Go!");
    // go
    startPistol.countDown();
    for (final Thread t : threads) {
        t.join(1000);
    }
}
Also used : ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConcurrentAccessException(javax.ejb.ConcurrentAccessException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with ConcurrentAccessException

use of javax.ejb.ConcurrentAccessException in project Payara by payara.

the class CMCSingletonContainer method _getContext.

/*
     * Findbugs complains that the lock acquired in this method is not
     *  unlocked on all paths in this method.
     *
     * Even though the method doesn't unlock the (possibly) acquired
     * lock, the lock is guaranteed to be unlocked in releaseContext()
     * even in the presence of (both checked and unchecked) exceptions.
     *
     * The general pattern used by various parts of the EJB container code is:
     *
     * try {
     *      container.preInvoke(inv);
     *      returnValue = container.intercept(inv);
     * } catch (Exception1 e1) {
     *      ...
     * } catch (Exception2 e2) {
     *      ...
     * } finally {
     *      container.postInvoke();
     * }
     *
     * Thus, it is clear that, BaseContainer.postInvoke() which in turn
     * calls releaseContext() will be called if container.preInvoke()
     * is called. This ensures that CMCSingletonContainer (this class)
     * releases the lock acquired by _getContext().
     *
     * Also, note that the above works even for loopback methods as
     * container.preInvoke() and container,postInvoke() will be called
     * before every bean method.
     *
     */
@Override
protected ComponentContext _getContext(EjbInvocation inv) {
    super._getContext(inv);
    InvocationInfo invInfo = inv.invocationInfo;
    MethodLockInfo lockInfo = (invInfo.methodLockInfo == null) ? defaultMethodLockInfo : invInfo.methodLockInfo;
    Lock theLock;
    if (lockInfo.isDistributed()) {
        if (_logger.isLoggable(Level.FINE)) {
            // log all lock operations
            theLock = (Lock) Proxy.newProxyInstance(loader, new Class<?>[] { Lock.class }, (proxy, method, args) -> {
                FencedLock fencedLock = clusteredLookup.getDistributedLock();
                _logger.log(Level.FINE, "DistributedLock, about to call {0}, Locked: {1}, Locked by Us: {2}, thread ID {3}", new Object[] { method.getName(), fencedLock.isLocked(), fencedLock.isLockedByCurrentThread(), Thread.currentThread().getId() });
                Object rv = method.invoke(fencedLock, args);
                _logger.log(Level.FINE, "DistributedLock, after to call {0}, Locked: {1}, Locked by Us: {2}, thread ID {3}", new Object[] { method.getName(), fencedLock.isLocked(), fencedLock.isLockedByCurrentThread(), Thread.currentThread().getId() });
                return rv;
            });
        } else {
            theLock = clusteredLookup.getDistributedLock();
        }
    } else {
        theLock = lockInfo.isReadLockedMethod() ? readLock : writeLock;
    }
    if ((rwLock.getReadHoldCount() > 0) && (!rwLock.isWriteLockedByCurrentThread())) {
        if (lockInfo.isWriteLockedMethod()) {
            throw new IllegalLoopbackException("Illegal Reentrant Access : Attempt to make " + "a loopback call on a Write Lock method '" + invInfo.targetMethod1 + "' while a Read lock is already held");
        }
    }
    /*
         * Please see comment at the beginning of the method.
         * Even though the method doesn't unlock the (possibly) acquired
         * lock, the lock is guaranteed to be unlocked in releaseContext()
         * even if exceptions were thrown in _getContext()
         */
    if (!lockInfo.hasTimeout() || ((lockInfo.hasTimeout() && (lockInfo.getTimeout() == BLOCK_INDEFINITELY)))) {
        theLock.lock();
    } else {
        try {
            boolean lockStatus = theLock.tryLock(lockInfo.getTimeout(), lockInfo.getTimeUnit());
            if (!lockStatus) {
                String msg = "Couldn't acquire a lock within " + lockInfo.getTimeout() + " " + lockInfo.getTimeUnit();
                if (lockInfo.getTimeout() == NO_BLOCKING) {
                    throw new ConcurrentAccessException(msg);
                } else {
                    throw new ConcurrentAccessTimeoutException(msg);
                }
            }
        } catch (InterruptedException inEx) {
            String msg = "Couldn't acquire a lock within " + lockInfo.getTimeout() + " " + lockInfo.getTimeUnit();
            ConcurrentAccessException cae = (lockInfo.getTimeout() == NO_BLOCKING) ? new ConcurrentAccessException(msg) : new ConcurrentAccessTimeoutException(msg);
            cae.initCause(inEx);
            throw cae;
        }
    }
    // Now that we have acquired the lock, remember it
    inv.setCMCLock(theLock);
    // Now that we have the lock return the singletonCtx
    return singletonCtx;
}
Also used : IllegalLoopbackException(javax.ejb.IllegalLoopbackException) InvocationInfo(com.sun.ejb.InvocationInfo) FencedLock(com.hazelcast.cp.lock.FencedLock) ConcurrentAccessException(javax.ejb.ConcurrentAccessException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) MethodLockInfo(com.sun.ejb.MethodLockInfo) FencedLock(com.hazelcast.cp.lock.FencedLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Aggregations

ConcurrentAccessException (javax.ejb.ConcurrentAccessException)7 ConcurrentAccessTimeoutException (javax.ejb.ConcurrentAccessTimeoutException)3 RemoteException (java.rmi.RemoteException)2 IllegalLoopbackException (javax.ejb.IllegalLoopbackException)2 ComponentInstance (org.jboss.as.ee.component.ComponentInstance)2 EJBComponent (org.jboss.as.ejb3.component.EJBComponent)2 Test (org.junit.Test)2 FencedLock (com.hazelcast.cp.lock.FencedLock)1 InvocationInfo (com.sun.ejb.InvocationInfo)1 MethodLockInfo (com.sun.ejb.MethodLockInfo)1 XMLDecoder (java.beans.XMLDecoder)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1