Search in sources :

Example 1 with UnavailableException

use of javax.resource.spi.UnavailableException in project tomee by apache.

the class MdbContainer method invoke.

public Object invoke(final Object deploymentId, final InterfaceType type, final Class callInterface, final Method method, final Object[] args, final Object primKey) throws OpenEJBException {
    final BeanContext beanContext = getBeanContext(deploymentId);
    final EndpointFactory endpointFactory = (EndpointFactory) beanContext.getContainerData();
    final MdbInstanceFactory instanceFactory = endpointFactory.getInstanceFactory();
    final Instance instance;
    try {
        instance = (Instance) instanceFactory.createInstance(true);
    } catch (final UnavailableException e) {
        throw new SystemException("Unable to create instance for invocation", e);
    }
    try {
        beforeDelivery(beanContext, instance, method, null);
        final Object value = invoke(instance, method, type, args);
        afterDelivery(instance);
        return value;
    } finally {
        instanceFactory.freeInstance(instance, true);
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) SystemException(org.apache.openejb.SystemException) EjbTransactionUtil.handleSystemException(org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException) SystemInstance(org.apache.openejb.loader.SystemInstance) UnavailableException(javax.resource.spi.UnavailableException)

Example 2 with UnavailableException

use of javax.resource.spi.UnavailableException in project wildfly by wildfly.

the class TransactionInflowWork method run.

public void run() {
    MessageEndpoint messageEndpoint;
    try {
        messageEndpoint = messageFactory.createEndpoint(null);
        if (messageEndpoint instanceof MessageListener) {
            log.tracef("Calling on message on endpoint '%s' with data message '%s'", messageEndpoint, msg);
            TransactionInflowTextMessage textMessage = new TransactionInflowTextMessage(msg);
            ((MessageListener) messageEndpoint).onMessage(textMessage);
        } else {
            throw new IllegalStateException("Not supported message endpoint: " + messageEndpoint);
        }
    } catch (UnavailableException ue) {
        String msg = String.format("Not capable to create message factory '%s' endpoint", messageFactory);
        throw new IllegalStateException(msg, ue);
    }
}
Also used : MessageEndpoint(javax.resource.spi.endpoint.MessageEndpoint) MessageListener(javax.jms.MessageListener) UnavailableException(javax.resource.spi.UnavailableException)

Example 3 with UnavailableException

use of javax.resource.spi.UnavailableException in project tomee by apache.

the class EndpointFactory method createEndpoint.

@Override
public MessageEndpoint createEndpoint(final XAResource xaResource, final long timeout) throws UnavailableException {
    if (timeout <= 0) {
        return createEndpoint(xaResource);
    }
    final long end = System.currentTimeMillis() + timeout;
    MessageEndpoint messageEndpoint = null;
    while (System.currentTimeMillis() <= end) {
        try {
            messageEndpoint = createEndpoint(xaResource);
            break;
        } catch (final Exception ex) {
        // ignore so we can keep trying
        }
    }
    if (messageEndpoint != null) {
        return messageEndpoint;
    } else {
        throw new UnavailableException("Unable to create end point within the specified timeout " + timeout);
    }
}
Also used : MessageEndpoint(javax.resource.spi.endpoint.MessageEndpoint) UnavailableException(javax.resource.spi.UnavailableException) UnavailableException(javax.resource.spi.UnavailableException)

Example 4 with UnavailableException

use of javax.resource.spi.UnavailableException in project tomee by apache.

the class EndpointHandler method deliverMessage.

public Object deliverMessage(final Method method, final Object[] args) throws Throwable {
    boolean callBeforeAfter = false;
    // verify current state
    switch(state) {
        case NONE:
            try {
                beforeDelivery(method);
            } catch (final ApplicationServerInternalException e) {
                throw (EJBException) new EJBException().initCause(e.getCause());
            }
            callBeforeAfter = true;
            state = State.METHOD_CALLED;
            break;
        case BEFORE_CALLED:
            state = State.METHOD_CALLED;
            break;
        case RELEASED:
            throw new IllegalStateException("Message endpoint factory has been released");
        case METHOD_CALLED:
        case SYSTEM_EXCEPTION:
            throw new IllegalStateException("The last message delivery must be completed with an afterDeliver before another message can be delivered");
    }
    Throwable throwable = null;
    Object value = null;
    try {
        // deliver the message
        value = container.invoke(instance, method, null, wrapMessageForAmq5(args));
    } catch (final SystemException se) {
        throwable = se.getRootCause() != null ? se.getRootCause() : se;
        state = State.SYSTEM_EXCEPTION;
    } catch (final ApplicationException ae) {
        throwable = ae.getRootCause() != null ? ae.getRootCause() : ae;
    } finally {
        // if the adapter is not using before/after, we must call afterDelivery to clean up
        if (callBeforeAfter) {
            try {
                afterDelivery();
            } catch (final ApplicationServerInternalException e) {
                throwable = throwable == null ? e.getCause() : throwable;
            } catch (final UnavailableException e) {
                throwable = throwable == null ? e : throwable;
            }
        }
    }
    if (throwable != null) {
        throwable.printStackTrace();
        if (isValidException(method, throwable)) {
            throw throwable;
        } else {
            throw new EJBException().initCause(throwable);
        }
    }
    return value;
}
Also used : ApplicationException(org.apache.openejb.ApplicationException) SystemException(org.apache.openejb.SystemException) UnavailableException(javax.resource.spi.UnavailableException) ApplicationServerInternalException(javax.resource.spi.ApplicationServerInternalException) EJBException(javax.ejb.EJBException)

Example 5 with UnavailableException

use of javax.resource.spi.UnavailableException 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

UnavailableException (javax.resource.spi.UnavailableException)5 MessageEndpoint (javax.resource.spi.endpoint.MessageEndpoint)2 BeanContext (org.apache.openejb.BeanContext)2 SystemException (org.apache.openejb.SystemException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 EJBException (javax.ejb.EJBException)1 MessageDrivenBean (javax.ejb.MessageDrivenBean)1 MessageListener (javax.jms.MessageListener)1 ApplicationServerInternalException (javax.resource.spi.ApplicationServerInternalException)1 ApplicationException (org.apache.openejb.ApplicationException)1 InstanceContext (org.apache.openejb.core.InstanceContext)1 ThreadContext (org.apache.openejb.core.ThreadContext)1 InterceptorStack (org.apache.openejb.core.interceptor.InterceptorStack)1 EjbTransactionUtil.handleSystemException (org.apache.openejb.core.transaction.EjbTransactionUtil.handleSystemException)1 SystemInstance (org.apache.openejb.loader.SystemInstance)1