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);
}
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations