Search in sources :

Example 1 with MessageDrivenBean

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);
        }
    }
}
Also used : MessageDrivenBean(javax.ejb.MessageDrivenBean) InterceptorData(org.apache.openejb.core.interceptor.InterceptorData) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack) ThreadContext(org.apache.openejb.core.ThreadContext) Operation(org.apache.openejb.core.Operation) Method(java.lang.reflect.Method)

Example 2 with MessageDrivenBean

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;
}
Also used : EjbInvocation(com.sun.ejb.EjbInvocation) MessageDrivenBean(javax.ejb.MessageDrivenBean) Utility.setContextClassLoader(com.sun.enterprise.util.Utility.setContextClassLoader) EJBLocalRemoteObject(com.sun.ejb.containers.EJBLocalRemoteObject) CreateException(javax.ejb.CreateException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with MessageDrivenBean

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();
}
Also used : MessageDrivenBean(javax.ejb.MessageDrivenBean) ComponentInstance(org.jboss.as.ee.component.ComponentInstance) MessageDrivenContext(javax.ejb.MessageDrivenContext)

Example 4 with MessageDrivenBean

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);
    }
}
Also used : HashMap(java.util.HashMap) ThreadContext(org.apache.openejb.core.ThreadContext) ArrayList(java.util.ArrayList) UnavailableException(javax.resource.spi.UnavailableException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) BeanContext(org.apache.openejb.BeanContext) InstanceContext(org.apache.openejb.core.InstanceContext) MessageDrivenBean(javax.ejb.MessageDrivenBean) InterceptorStack(org.apache.openejb.core.interceptor.InterceptorStack)

Aggregations

MessageDrivenBean (javax.ejb.MessageDrivenBean)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 ThreadContext (org.apache.openejb.core.ThreadContext)2 InterceptorStack (org.apache.openejb.core.interceptor.InterceptorStack)2 EjbInvocation (com.sun.ejb.EjbInvocation)1 EJBLocalRemoteObject (com.sun.ejb.containers.EJBLocalRemoteObject)1 Utility.setContextClassLoader (com.sun.enterprise.util.Utility.setContextClassLoader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 CreateException (javax.ejb.CreateException)1 MessageDrivenContext (javax.ejb.MessageDrivenContext)1 UnavailableException (javax.resource.spi.UnavailableException)1 BeanContext (org.apache.openejb.BeanContext)1 InstanceContext (org.apache.openejb.core.InstanceContext)1 Operation (org.apache.openejb.core.Operation)1 InterceptorData (org.apache.openejb.core.interceptor.InterceptorData)1 ComponentInstance (org.jboss.as.ee.component.ComponentInstance)1