Search in sources :

Example 1 with TransactionalExecutionTemplate

use of org.mule.runtime.core.api.execution.TransactionalExecutionTemplate in project mule by mulesoft.

the class FlowProcessingPhase method runPhase.

@Override
public void runPhase(final FlowProcessingPhaseTemplate flowProcessingPhaseTemplate, final MessageProcessContext messageProcessContext, final PhaseResultNotifier phaseResultNotifier) {
    Runnable flowExecutionWork = () -> {
        try {
            FlowConstruct flowConstruct = registry.<FlowConstruct>lookupByName(messageProcessContext.getMessageSource().getRootContainerLocation().toString()).get();
            try {
                final AtomicReference exceptionThrownDuringFlowProcessing = new AtomicReference();
                TransactionalExecutionTemplate<CoreEvent> transactionTemplate = createTransactionalExecutionTemplate(muleContext, messageProcessContext.getTransactionConfig().orElse(new MuleTransactionConfig()));
                CoreEvent response = transactionTemplate.execute(() -> {
                    try {
                        Object message = flowProcessingPhaseTemplate.getOriginalMessage();
                        if (message == null) {
                            return null;
                        }
                        CoreEvent muleEvent = flowProcessingPhaseTemplate.getEvent();
                        muleEvent = flowProcessingPhaseTemplate.beforeRouteEvent(muleEvent);
                        muleEvent = flowProcessingPhaseTemplate.routeEvent(muleEvent);
                        muleEvent = flowProcessingPhaseTemplate.afterRouteEvent(muleEvent);
                        sendResponseIfNeccessary(messageProcessContext.getMessageSource(), flowConstruct, muleEvent, flowProcessingPhaseTemplate);
                        return muleEvent;
                    } catch (Exception e) {
                        exceptionThrownDuringFlowProcessing.set(e);
                        throw e;
                    }
                });
                if (exceptionThrownDuringFlowProcessing.get() != null && !(exceptionThrownDuringFlowProcessing.get() instanceof ResponseDispatchException)) {
                    sendResponseIfNeccessary(messageProcessContext.getMessageSource(), flowConstruct, response, flowProcessingPhaseTemplate);
                }
                flowProcessingPhaseTemplate.afterSuccessfulProcessingFlow(response);
            } catch (ResponseDispatchException e) {
                flowProcessingPhaseTemplate.afterFailureProcessingFlow(e);
            } catch (MessagingException e) {
                sendFailureResponseIfNeccessary(messageProcessContext.getMessageSource(), flowConstruct, e, flowProcessingPhaseTemplate);
                flowProcessingPhaseTemplate.afterFailureProcessingFlow(e);
            }
            phaseResultNotifier.phaseSuccessfully();
        } catch (Exception e) {
            MuleException me = new DefaultMuleException(e);
            try {
                flowProcessingPhaseTemplate.afterFailureProcessingFlow(me);
            } catch (MuleException e1) {
                logger.warn("Failure during exception processing in flow template: " + e.getMessage());
                if (logger.isDebugEnabled()) {
                    logger.debug("Failure during exception processing in flow template: ", e);
                }
            }
            phaseResultNotifier.phaseFailure(e);
        }
    };
    if (messageProcessContext.supportsAsynchronousProcessing()) {
        try {
            messageProcessContext.getFlowExecutionExecutor().execute(flowExecutionWork);
        } catch (SchedulerBusyException e) {
            phaseResultNotifier.phaseFailure(e);
        }
    } else {
        flowExecutionWork.run();
    }
}
Also used : MessagingException(org.mule.runtime.core.internal.exception.MessagingException) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) AtomicReference(java.util.concurrent.atomic.AtomicReference) MuleTransactionConfig(org.mule.runtime.core.api.transaction.MuleTransactionConfig) SchedulerBusyException(org.mule.runtime.api.scheduler.SchedulerBusyException) ResponseDispatchException(org.mule.runtime.core.privileged.exception.ResponseDispatchException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) MuleException(org.mule.runtime.api.exception.MuleException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) TransactionalExecutionTemplate.createTransactionalExecutionTemplate(org.mule.runtime.core.api.execution.TransactionalExecutionTemplate.createTransactionalExecutionTemplate) TransactionalExecutionTemplate(org.mule.runtime.core.api.execution.TransactionalExecutionTemplate) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ResponseDispatchException(org.mule.runtime.core.privileged.exception.ResponseDispatchException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) MuleException(org.mule.runtime.api.exception.MuleException) SchedulerBusyException(org.mule.runtime.api.scheduler.SchedulerBusyException)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)1 DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)1 MuleException (org.mule.runtime.api.exception.MuleException)1 SchedulerBusyException (org.mule.runtime.api.scheduler.SchedulerBusyException)1 FlowConstruct (org.mule.runtime.core.api.construct.FlowConstruct)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1 TransactionalExecutionTemplate (org.mule.runtime.core.api.execution.TransactionalExecutionTemplate)1 TransactionalExecutionTemplate.createTransactionalExecutionTemplate (org.mule.runtime.core.api.execution.TransactionalExecutionTemplate.createTransactionalExecutionTemplate)1 MuleTransactionConfig (org.mule.runtime.core.api.transaction.MuleTransactionConfig)1 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)1 ResponseDispatchException (org.mule.runtime.core.privileged.exception.ResponseDispatchException)1