Search in sources :

Example 6 with Duration

use of org.apache.openejb.util.Duration in project tomee by apache.

the class MethodConcurrencyBuilder method applyConcurrencyAttributes.

public static void applyConcurrencyAttributes(final BeanContext beanContext, final List<MethodConcurrencyInfo> methodConcurrencyInfos) throws OpenEJBException {
    if (beanContext.isBeanManagedConcurrency()) {
        return;
    }
    final Logger log = Logger.getInstance(LogCategory.OPENEJB_STARTUP.createChild("attributes"), MethodConcurrencyBuilder.class);
    final List<MethodConcurrencyInfo> lockInfos = new ArrayList<MethodConcurrencyInfo>();
    final List<MethodConcurrencyInfo> accessTimeoutInfos = new ArrayList<MethodConcurrencyInfo>();
    MethodConcurrencyBuilder.normalize(methodConcurrencyInfos, lockInfos, accessTimeoutInfos);
    Map<Method, MethodAttributeInfo> attributes;
    // handle @Lock
    attributes = MethodInfoUtil.resolveAttributes(lockInfos, beanContext);
    if (log.isDebugEnabled()) {
        for (final Map.Entry<Method, MethodAttributeInfo> entry : attributes.entrySet()) {
            final Method method = entry.getKey();
            final MethodConcurrencyInfo value = (MethodConcurrencyInfo) entry.getValue();
            log.debug("Lock: " + method + " -- " + MethodInfoUtil.toString(value.methods.get(0)) + " " + value.concurrencyAttribute);
        }
    }
    for (final Map.Entry<Method, MethodAttributeInfo> entry : attributes.entrySet()) {
        final MethodConcurrencyInfo value = (MethodConcurrencyInfo) entry.getValue();
        final MethodContext methodContext = beanContext.getMethodContext(entry.getKey());
        final String s = value.concurrencyAttribute.toUpperCase();
        methodContext.setLockType(LockType.valueOf(s));
    }
    // handle @AccessTimeout
    attributes = MethodInfoUtil.resolveAttributes(accessTimeoutInfos, beanContext);
    if (log.isDebugEnabled()) {
        for (final Map.Entry<Method, MethodAttributeInfo> entry : attributes.entrySet()) {
            final Method method = entry.getKey();
            final MethodConcurrencyInfo value = (MethodConcurrencyInfo) entry.getValue();
            log.debug("AccessTimeout: " + method + " -- " + MethodInfoUtil.toString(value.methods.get(0)) + " " + " " + value.accessTimeout.time + " " + value.accessTimeout.unit);
        }
    }
    for (final Map.Entry<Method, MethodAttributeInfo> entry : attributes.entrySet()) {
        final MethodConcurrencyInfo value = (MethodConcurrencyInfo) entry.getValue();
        final MethodContext methodContext = beanContext.getMethodContext(entry.getKey());
        final Duration accessTimeout = new Duration(value.accessTimeout.time, TimeUnit.valueOf(value.accessTimeout.unit));
        methodContext.setAccessTimeout(accessTimeout);
    }
}
Also used : ArrayList(java.util.ArrayList) MethodContext(org.apache.openejb.MethodContext) Duration(org.apache.openejb.util.Duration) Method(java.lang.reflect.Method) Logger(org.apache.openejb.util.Logger) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with Duration

use of org.apache.openejb.util.Duration in project tomee by apache.

the class JmsTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    // create a transaction manager
    final GeronimoTransactionManager transactionManager = new GeronimoTransactionManager();
    // create the ActiveMQ resource adapter instance
    ra = new ActiveMQResourceAdapter();
    // initialize properties
    ra.setServerUrl(brokerAddress);
    ra.setBrokerXmlConfig(brokerXmlConfig);
    ra.setStartupTimeout(new Duration(10, TimeUnit.SECONDS));
    // create a thead pool for ActiveMQ
    final Executor threadPool = Executors.newFixedThreadPool(30);
    // create a work manager which ActiveMQ uses to dispatch message delivery jobs
    final TransactionContextHandler txWorkContextHandler = new TransactionContextHandler(transactionManager);
    final GeronimoWorkManager workManager = new GeronimoWorkManager(threadPool, threadPool, threadPool, Collections.<WorkContextHandler>singletonList(txWorkContextHandler));
    // wrap the work mananger and transaction manager in a bootstrap context (connector spec thing)
    final BootstrapContext bootstrapContext = new GeronimoBootstrapContext(workManager, transactionManager, transactionManager);
    // Create a ConnectionFactory
    connectionFactory = new ActiveMQConnectionFactory(brokerAddress);
    ra.setConnectionFactory(connectionFactory);
    // start the resource adapter
    try {
        ra.start(bootstrapContext);
    } catch (final ResourceAdapterInternalException e) {
        throw new OpenEJBException(e);
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) OpenEJBException(org.apache.openejb.OpenEJBException) Executor(java.util.concurrent.Executor) GeronimoBootstrapContext(org.apache.geronimo.connector.GeronimoBootstrapContext) TransactionContextHandler(org.apache.geronimo.connector.work.TransactionContextHandler) GeronimoWorkManager(org.apache.geronimo.connector.work.GeronimoWorkManager) ActiveMQResourceAdapter(org.apache.openejb.resource.activemq.ActiveMQResourceAdapter) Duration(org.apache.openejb.util.Duration) GeronimoBootstrapContext(org.apache.geronimo.connector.GeronimoBootstrapContext) BootstrapContext(javax.resource.spi.BootstrapContext) GeronimoTransactionManager(org.apache.geronimo.transaction.manager.GeronimoTransactionManager) ResourceAdapterInternalException(javax.resource.spi.ResourceAdapterInternalException)

Example 8 with Duration

use of org.apache.openejb.util.Duration in project tomee by apache.

the class SingletonContainer method _invoke.

protected Object _invoke(final Method callMethod, final Method runMethod, final Object[] args, final Instance instance, final ThreadContext callContext, final InterfaceType callType) throws OpenEJBException {
    final BeanContext beanContext = callContext.getBeanContext();
    final Duration accessTimeout = getAccessTimeout(beanContext, runMethod);
    final boolean read = javax.ejb.LockType.READ.equals(beanContext.getConcurrencyAttribute(runMethod));
    final Lock lock = aquireLock(read, accessTimeout, instance, runMethod);
    Object returnValue;
    try {
        final TransactionPolicy txPolicy = createTransactionPolicy(beanContext.getTransactionType(callMethod, callType), callContext);
        returnValue = null;
        try {
            if (callType == InterfaceType.SERVICE_ENDPOINT) {
                callContext.setCurrentOperation(Operation.BUSINESS_WS);
                returnValue = invokeWebService(args, beanContext, runMethod, instance);
            } else {
                final List<InterceptorData> interceptors = beanContext.getMethodInterceptors(runMethod);
                final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, runMethod, callType == InterfaceType.TIMEOUT ? Operation.TIMEOUT : Operation.BUSINESS, interceptors, instance.interceptors);
                returnValue = interceptorStack.invoke(args);
            }
        } catch (final Throwable e) {
            // handle reflection exception
            final ExceptionType type = beanContext.getExceptionType(e);
            if (type == ExceptionType.SYSTEM) {
                /* System Exception ****************************/
                // The bean instance is not put into the pool via instanceManager.poolInstance
                // and therefore the instance will be garbage collected and destroyed.
                // For this reason the discardInstance method of the StatelessInstanceManager
                // does nothing.
                handleSystemException(txPolicy, e, callContext);
            } else {
                /* Application Exception ***********************/
                handleApplicationException(txPolicy, e, type == ExceptionType.APPLICATION_ROLLBACK);
            }
        } finally {
            afterInvoke(txPolicy, callContext);
        }
    } finally {
        lock.unlock();
    }
    return returnValue;
}
Also used : BeanContext(org.apache.openejb.BeanContext) ExceptionType(org.apache.openejb.core.ExceptionType) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) EjbTransactionUtil.createTransactionPolicy(org.apache.openejb.core.transaction.EjbTransactionUtil.createTransactionPolicy) Duration(org.apache.openejb.util.Duration) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject) Lock(java.util.concurrent.locks.Lock)

Example 9 with Duration

use of org.apache.openejb.util.Duration in project tomee by apache.

the class StatelessInstanceManager method getDuration.

private Duration getDuration(final Options options, final String property, final Duration defaultValue, final TimeUnit defaultUnit) {
    final String s = options.get(property, defaultValue.toString());
    final Duration duration = new Duration(s);
    if (duration.getUnit() == null) {
        duration.setUnit(defaultUnit);
    }
    return duration;
}
Also used : Duration(org.apache.openejb.util.Duration)

Example 10 with Duration

use of org.apache.openejb.util.Duration in project tomee by apache.

the class StatefulContainer method obtainInstance.

@SuppressWarnings("LockAcquiredButNotSafelyReleased")
private Instance obtainInstance(final Object primaryKey, final ThreadContext callContext, final Method callMethod, final boolean checkOutIfNecessary) throws OpenEJBException {
    if (primaryKey == null) {
        throw new SystemException(new NullPointerException("Cannot obtain an instance of the stateful session bean with a null session id"));
    }
    final Transaction currentTransaction = getTransaction(callContext);
    // Find the instance
    Instance instance;
    synchronized (this) {
        instance = checkedOutInstances.get(primaryKey);
        if (instance == null) {
            // no need to check for extended persistence contexts it shouldn't happen
            try {
                instance = cache.checkOut(primaryKey, checkOutIfNecessary);
            } catch (final OpenEJBException e) {
                throw e;
            } catch (final Exception e) {
                throw new SystemException("Unexpected load exception", e);
            }
            // Did we find the instance?
            if (instance == null) {
                throw new InvalidateReferenceException(new NoSuchObjectException("Not Found"));
            }
            // remember instance until it is returned to the cache
            checkedOutInstances.put(primaryKey, instance);
        }
    }
    final Duration accessTimeout = getAccessTimeout(instance.beanContext, callMethod);
    final LockFactory.StatefulLock currLock = instance.getLock();
    final boolean lockAcquired;
    if (accessTimeout == null || accessTimeout.getTime() < 0) {
        // wait indefinitely for a lock
        currLock.lock();
        lockAcquired = true;
    } else if (accessTimeout.getTime() == 0) {
        // concurrent calls are not allowed, lock only once
        lockAcquired = currLock.tryLock();
    } else {
        // try to get a lock within the specified period.
        try {
            lockAcquired = currLock.tryLock(accessTimeout.getTime(), accessTimeout.getUnit());
        } catch (final InterruptedException e) {
            throw new ApplicationException("Unable to get lock.", e);
        }
    }
    // Did we acquire the lock to the current execution?
    if (!lockAcquired) {
        throw new ApplicationException(new ConcurrentAccessTimeoutException("Unable to get lock."));
    }
    if (instance.getTransaction() != null) {
        if (!instance.getTransaction().equals(currentTransaction) && !instance.getLock().tryLock()) {
            throw new ApplicationException(new RemoteException("Instance is in a transaction and cannot be invoked outside that transaction.  See EJB 3.0 Section 4.4.4"));
        }
    } else {
        instance.setTransaction(currentTransaction);
    }
    // Mark the instance in use so we can detect reentrant calls
    instance.setInUse(true);
    return instance;
}
Also used : OpenEJBException(org.apache.openejb.OpenEJBException) SystemInstance(org.apache.openejb.loader.SystemInstance) Duration(org.apache.openejb.util.Duration) LoginException(javax.security.auth.login.LoginException) NamingException(javax.naming.NamingException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) EJBAccessException(javax.ejb.EJBAccessException) RemoveException(javax.ejb.RemoveException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) OpenEJBException(org.apache.openejb.OpenEJBException) RemoteException(java.rmi.RemoteException) EJBException(javax.ejb.EJBException) SystemException(org.apache.openejb.SystemException) NoSuchObjectException(java.rmi.NoSuchObjectException) EntityManagerAlreadyRegisteredException(org.apache.openejb.persistence.EntityManagerAlreadyRegisteredException) ApplicationException(org.apache.openejb.ApplicationException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) InvalidateReferenceException(org.apache.openejb.InvalidateReferenceException) ApplicationException(org.apache.openejb.ApplicationException) SystemException(org.apache.openejb.SystemException) Transaction(javax.transaction.Transaction) SuspendedTransaction(org.apache.openejb.core.transaction.BeanTransactionPolicy.SuspendedTransaction) EjbUserTransaction(org.apache.openejb.core.transaction.EjbUserTransaction) NoSuchObjectException(java.rmi.NoSuchObjectException) ConcurrentAccessTimeoutException(javax.ejb.ConcurrentAccessTimeoutException) RemoteException(java.rmi.RemoteException)

Aggregations

Duration (org.apache.openejb.util.Duration)20 HashMap (java.util.HashMap)6 Map (java.util.Map)5 File (java.io.File)4 NamingException (javax.naming.NamingException)4 OpenEJBException (org.apache.openejb.OpenEJBException)4 Test (org.junit.Test)4 Method (java.lang.reflect.Method)3 Properties (java.util.Properties)3 InitialContext (javax.naming.InitialContext)3 BeanContext (org.apache.openejb.BeanContext)3 Calculator (org.apache.openejb.itest.failover.ejb.Calculator)3 StandaloneServer (org.apache.openejb.server.control.StandaloneServer)3 PrintStream (java.io.PrintStream)2 RemoteException (java.rmi.RemoteException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 ExecutorService (java.util.concurrent.ExecutorService)2 TimeoutException (java.util.concurrent.TimeoutException)2 ConcurrentAccessTimeoutException (javax.ejb.ConcurrentAccessTimeoutException)2