use of javax.ejb.MessageDrivenBean in project tomee by apache.
the class MdbInstanceFactory method freeInstance.
/**
* Frees an instance no longer needed by the resource adapter. This method makes all the necessary lifecycle
* callbacks and decrements the instance count. This method should not be used to disposed of beans that have
* thrown a system exception. Instead the discardInstance method should be called.
*
* @param instance the bean instance to free
* @param ignoredInstanceCount
*/
public void freeInstance(final Instance instance, final boolean ignoredInstanceCount) {
if (instance == null) {
throw new NullPointerException("bean is null");
}
// decrement the instance count
if (!ignoredInstanceCount) {
synchronized (this) {
instanceCount--;
}
}
final ThreadContext callContext = ThreadContext.getThreadContext();
final Operation originalOperation = callContext == null ? null : callContext.getCurrentOperation();
final BaseContext.State[] originalAllowedStates = callContext == null ? null : callContext.getCurrentAllowedStates();
try {
// call post destroy method
if (callContext != null) {
callContext.setCurrentOperation(Operation.PRE_DESTROY);
}
final Method remove = instance.bean instanceof MessageDrivenBean ? MessageDrivenBean.class.getMethod("ejbRemove") : null;
final List<InterceptorData> callbackInterceptors = beanContext.getCallbackInterceptors();
final InterceptorStack interceptorStack = new InterceptorStack(instance.bean, remove, Operation.PRE_DESTROY, callbackInterceptors, instance.interceptors);
interceptorStack.invoke();
if (instance.creationalContext != null) {
instance.creationalContext.release();
}
} catch (final Throwable re) {
MdbInstanceFactory.logger.error("The bean instance " + instance.bean + " threw a system exception:" + re, re);
} finally {
if (callContext != null) {
callContext.setCurrentOperation(originalOperation);
callContext.setCurrentAllowedStates(originalAllowedStates);
}
}
}
use of javax.ejb.MessageDrivenBean in project Payara by payara.
the class MessageBeanContainer method createMessageDrivenEJB.
/**
* Instantiate and initialize a message-driven bean instance.
*/
private MessageBeanContextImpl createMessageDrivenEJB() throws CreateException {
EjbInvocation ejbInvocation = null;
MessageBeanContextImpl context = null;
ClassLoader originalClassLoader = null;
try {
// Set application class loader before invoking instance.
originalClassLoader = setContextClassLoader(getClassLoader());
context = (MessageBeanContextImpl) createEjbInstanceAndContext();
Object ejb = context.getEJB();
// java:comp/env lookups are allowed from here on...
ejbInvocation = createEjbInvocation(ejb, context);
ejbInvocation.isMessageDriven = true;
invocationManager.preInvoke(ejbInvocation);
if (ejb instanceof MessageDrivenBean) {
// setMessageDrivenContext will be called without a Tx
// as required by the spec
((MessageDrivenBean) ejb).setMessageDrivenContext(context);
}
// Perform injection right after where setMessageDrivenContext would be called. This is important since
// injection methods have the same "operations allowed" permissions as setMessageDrivenContext.
injectEjbInstance(context);
// Set flag in context so UserTransaction can be used from ejbCreate. Didn't want to add
// a new state to lifecycle since that would require either changing lots of code in
// EJBContextImpl or re-implementing all the context methods within MessageBeanContextImpl.
context.setContextCalled();
// Call ejbCreate OR @PostConstruct on the bean.
intercept(POST_CONSTRUCT, context);
ejbProbeNotifier.ejbBeanCreatedEvent(getContainerId(), containerInfo.appName, containerInfo.modName, containerInfo.ejbName);
// Set the state to POOLED after ejbCreate so that EJBContext methods not allowed will throw exceptions
context.setState(POOLED);
} catch (Throwable t) {
_logger.log(SEVERE, "containers.mdb.ejb_creation_exception", new Object[] { appEJBName_, t.toString() });
if (t instanceof InvocationTargetException) {
_logger.log(Level.SEVERE, t.getClass().getName(), t.getCause());
}
_logger.log(SEVERE, t.getClass().getName(), t);
CreateException ce = new CreateException("Could not create Message-Driven EJB");
ce.initCause(t);
throw ce;
} finally {
if (originalClassLoader != null) {
setContextClassLoader(originalClassLoader);
}
if (ejbInvocation != null) {
invocationManager.postInvoke(ejbInvocation);
}
}
return context;
}
use of javax.ejb.MessageDrivenBean in project wildfly by wildfly.
the class MessageDrivenBeanSetMessageDrivenContextInterceptor method processInvocation.
@Override
public Object processInvocation(InterceptorContext context) throws Exception {
final MessageDrivenComponentInstance componentInstance = (MessageDrivenComponentInstance) context.getPrivateData(ComponentInstance.class);
final MessageDrivenContext messageDrivenContext = (MessageDrivenContext) componentInstance.getEjbContext();
((MessageDrivenBean) context.getTarget()).setMessageDrivenContext(messageDrivenContext);
return context.proceed();
}
use of javax.ejb.MessageDrivenBean in project tomee by apache.
the class MdbInstanceFactory method constructBean.
private Object constructBean() throws UnavailableException {
final BeanContext beanContext = this.beanContext;
final ThreadContext callContext = new ThreadContext(beanContext, null, Operation.INJECTION);
final ThreadContext oldContext = ThreadContext.enter(callContext);
try {
final InstanceContext context = beanContext.newInstance();
if (context.getBean() instanceof MessageDrivenBean) {
callContext.setCurrentOperation(Operation.CREATE);
final Method create = beanContext.getCreateMethod();
final InterceptorStack ejbCreate = new InterceptorStack(context.getBean(), create, Operation.CREATE, new ArrayList(), new HashMap());
ejbCreate.invoke();
}
return new Instance(context.getBean(), context.getInterceptors(), context.getCreationalContext());
} catch (Throwable e) {
if (e instanceof InvocationTargetException) {
e = ((InvocationTargetException) e).getTargetException();
}
final String message = "The bean instance threw a system exception:" + e;
MdbInstanceFactory.logger.error(message, e);
throw new UnavailableException(message, e);
} finally {
ThreadContext.exit(oldContext);
}
}
Aggregations