Search in sources :

Example 16 with UnitOfWork

use of org.apache.camel.spi.UnitOfWork in project camel by apache.

the class CamelInternalProcessor method process.

@Override
public boolean process(Exchange exchange, AsyncCallback callback) {
    if (processor == null || !continueProcessing(exchange)) {
        // no processor or we should not continue then we are done
        callback.done(true);
        return true;
    }
    final List<Object> states = new ArrayList<Object>(advices.size());
    for (CamelInternalProcessorAdvice task : advices) {
        try {
            Object state = task.before(exchange);
            states.add(state);
        } catch (Throwable e) {
            exchange.setException(e);
            callback.done(true);
            return true;
        }
    }
    // create internal callback which will execute the advices in reverse order when done
    callback = new InternalCallback(states, exchange, callback);
    // UNIT_OF_WORK_PROCESS_SYNC is @deprecated and we should remove it from Camel 3.0
    Object synchronous = exchange.removeProperty(Exchange.UNIT_OF_WORK_PROCESS_SYNC);
    if (exchange.isTransacted() || synchronous != null) {
        // must be synchronized for transacted exchanges
        if (LOG.isTraceEnabled()) {
            if (exchange.isTransacted()) {
                LOG.trace("Transacted Exchange must be routed synchronously for exchangeId: {} -> {}", exchange.getExchangeId(), exchange);
            } else {
                LOG.trace("Synchronous UnitOfWork Exchange must be routed synchronously for exchangeId: {} -> {}", exchange.getExchangeId(), exchange);
            }
        }
        // ----------------------------------------------------------
        try {
            processor.process(exchange);
        } catch (Throwable e) {
            exchange.setException(e);
        }
        // ----------------------------------------------------------
        // CAMEL END USER - DEBUG ME HERE +++ END +++
        // ----------------------------------------------------------
        callback.done(true);
        return true;
    } else {
        final UnitOfWork uow = exchange.getUnitOfWork();
        // allow unit of work to wrap callback in case it need to do some special work
        // for example the MDCUnitOfWork
        AsyncCallback async = callback;
        if (uow != null) {
            async = uow.beforeProcess(processor, exchange, callback);
        }
        // ----------------------------------------------------------
        if (LOG.isTraceEnabled()) {
            LOG.trace("Processing exchange for exchangeId: {} -> {}", exchange.getExchangeId(), exchange);
        }
        boolean sync = processor.process(exchange, async);
        // execute any after processor work (in current thread, not in the callback)
        if (uow != null) {
            uow.afterProcess(processor, exchange, callback, sync);
        }
        if (LOG.isTraceEnabled()) {
            LOG.trace("Exchange processed and is continued routed {} for exchangeId: {} -> {}", new Object[] { sync ? "synchronously" : "asynchronously", exchange.getExchangeId(), exchange });
        }
        return sync;
    }
}
Also used : UnitOfWork(org.apache.camel.spi.UnitOfWork) AsyncCallback(org.apache.camel.AsyncCallback) ArrayList(java.util.ArrayList)

Aggregations

UnitOfWork (org.apache.camel.spi.UnitOfWork)16 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)4 AsyncCallback (org.apache.camel.AsyncCallback)3 AsyncProcessor (org.apache.camel.AsyncProcessor)3 Processor (org.apache.camel.Processor)3 Exchange (org.apache.camel.Exchange)2 Message (org.apache.camel.Message)2 DefaultExchange (org.apache.camel.impl.DefaultExchange)2 ArrayList (java.util.ArrayList)1 Channel (org.apache.camel.Channel)1 Consumer (org.apache.camel.Consumer)1 NonManagedService (org.apache.camel.NonManagedService)1 Producer (org.apache.camel.Producer)1 RuntimeCamelCatalog (org.apache.camel.catalog.RuntimeCamelCatalog)1 ConsumerCache (org.apache.camel.impl.ConsumerCache)1 DefaultEndpointRegistry (org.apache.camel.impl.DefaultEndpointRegistry)1 DefaultTransformerRegistry (org.apache.camel.impl.DefaultTransformerRegistry)1 DefaultUnitOfWork (org.apache.camel.impl.DefaultUnitOfWork)1 DefaultValidatorRegistry (org.apache.camel.impl.DefaultValidatorRegistry)1 ProducerCache (org.apache.camel.impl.ProducerCache)1