Search in sources :

Example 1 with SubUnitOfWork

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

the class DefaultUnitOfWork method endSubUnitOfWork.

@Override
public void endSubUnitOfWork(Exchange exchange) {
    if (log.isTraceEnabled()) {
        log.trace("endSubUnitOfWork exchangeId: {}", exchange.getExchangeId());
    }
    if (subUnitOfWorks == null || subUnitOfWorks.isEmpty()) {
        return;
    }
    // pop last sub unit of work as its now ended
    SubUnitOfWork subUoW = subUnitOfWorks.pop();
    if (subUoW.isFailed()) {
        // the sub unit of work failed so set an exception containing all the caused exceptions
        // and mark the exchange for rollback only
        // if there are multiple exceptions then wrap those into another exception with them all
        Exception cause;
        List<Exception> list = subUoW.getExceptions();
        if (list != null) {
            if (list.size() == 1) {
                cause = list.get(0);
            } else {
                cause = new CamelUnitOfWorkException(exchange, list);
            }
            exchange.setException(cause);
        }
        // mark it as rollback and that the unit of work is exhausted. This ensures that we do not try
        // to redeliver this exception (again)
        exchange.setProperty(Exchange.ROLLBACK_ONLY, true);
        exchange.setProperty(Exchange.UNIT_OF_WORK_EXHAUSTED, true);
        // and remove any indications of error handled which will prevent this exception to be noticed
        // by the error handler which we want to react with the result of the sub unit of work
        exchange.setProperty(Exchange.ERRORHANDLER_HANDLED, null);
        exchange.setProperty(Exchange.FAILURE_HANDLED, null);
        if (log.isTraceEnabled()) {
            log.trace("endSubUnitOfWork exchangeId: {} with {} caused exceptions.", exchange.getExchangeId(), list != null ? list.size() : 0);
        }
    }
}
Also used : SubUnitOfWork(org.apache.camel.spi.SubUnitOfWork) CamelUnitOfWorkException(org.apache.camel.CamelUnitOfWorkException) CamelUnitOfWorkException(org.apache.camel.CamelUnitOfWorkException)

Aggregations

CamelUnitOfWorkException (org.apache.camel.CamelUnitOfWorkException)1 SubUnitOfWork (org.apache.camel.spi.SubUnitOfWork)1