use of org.apache.camel.spi.UnitOfWork in project camel by apache.
the class ExpressionBuilder method routeIdExpression.
/**
* Returns an Expression for the route id
*/
public static Expression routeIdExpression() {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
String answer = null;
UnitOfWork uow = exchange.getUnitOfWork();
RouteContext rc = uow != null ? uow.getRouteContext() : null;
if (rc != null) {
answer = rc.getRoute().getId();
}
if (answer == null) {
// fallback and get from route id on the exchange
answer = exchange.getFromRouteId();
}
return answer;
}
@Override
public String toString() {
return "routeId";
}
};
}
use of org.apache.camel.spi.UnitOfWork in project camel by apache.
the class MulticastProcessor method createUnitOfWorkProcessor.
/**
* Strategy to create the unit of work to be used for the sub route
*
* @param routeContext the route context
* @param processor the processor
* @param exchange the exchange
* @return the unit of work processor
*/
protected Processor createUnitOfWorkProcessor(RouteContext routeContext, Processor processor, Exchange exchange) {
CamelInternalProcessor internal = new CamelInternalProcessor(processor);
// and wrap it in a unit of work so the UoW is on the top, so the entire route will be in the same UoW
UnitOfWork parent = exchange.getProperty(Exchange.PARENT_UNIT_OF_WORK, UnitOfWork.class);
if (parent != null) {
internal.addAdvice(new CamelInternalProcessor.ChildUnitOfWorkProcessorAdvice(routeContext, parent));
} else {
internal.addAdvice(new CamelInternalProcessor.UnitOfWorkProcessorAdvice(routeContext));
}
return internal;
}
use of org.apache.camel.spi.UnitOfWork in project camel by apache.
the class RedeliveryErrorHandler method handleException.
protected void handleException(Exchange exchange, RedeliveryData data, boolean isDeadLetterChannel) {
Exception e = exchange.getException();
// store the original caused exception in a property, so we can restore it later
exchange.setProperty(Exchange.EXCEPTION_CAUGHT, e);
// find the error handler to use (if any)
OnExceptionDefinition exceptionPolicy = getExceptionPolicy(exchange, e);
if (exceptionPolicy != null) {
data.currentRedeliveryPolicy = exceptionPolicy.createRedeliveryPolicy(exchange.getContext(), data.currentRedeliveryPolicy);
data.handledPredicate = exceptionPolicy.getHandledPolicy();
data.continuedPredicate = exceptionPolicy.getContinuedPolicy();
data.retryWhilePredicate = exceptionPolicy.getRetryWhilePolicy();
data.useOriginalInMessage = exceptionPolicy.getUseOriginalMessagePolicy() != null && exceptionPolicy.getUseOriginalMessagePolicy();
// route specific failure handler?
Processor processor = null;
UnitOfWork uow = exchange.getUnitOfWork();
if (uow != null && uow.getRouteContext() != null) {
String routeId = uow.getRouteContext().getRoute().getId();
processor = exceptionPolicy.getErrorHandler(routeId);
} else if (!exceptionPolicy.getErrorHandlers().isEmpty()) {
// note this should really not happen, but we have this code as a fail safe
// to be backwards compatible with the old behavior
log.warn("Cannot determine current route from Exchange with id: {}, will fallback and use first error handler.", exchange.getExchangeId());
processor = exceptionPolicy.getErrorHandlers().iterator().next();
}
if (processor != null) {
data.failureProcessor = processor;
}
// route specific on redelivery?
processor = exceptionPolicy.getOnRedelivery();
if (processor != null) {
data.onRedeliveryProcessor = processor;
}
// route specific on exception occurred?
processor = exceptionPolicy.getOnExceptionOccurred();
if (processor != null) {
data.onExceptionProcessor = processor;
}
}
// only log if not failure handled or not an exhausted unit of work
if (!ExchangeHelper.isFailureHandled(exchange) && !ExchangeHelper.isUnitOfWorkExhausted(exchange)) {
String msg = "Failed delivery for " + ExchangeHelper.logIds(exchange) + ". On delivery attempt: " + data.redeliveryCounter + " caught: " + e;
logFailedDelivery(true, false, false, false, isDeadLetterChannel, exchange, msg, data, e);
}
data.redeliveryCounter = incrementRedeliveryCounter(exchange, e, data);
}
use of org.apache.camel.spi.UnitOfWork in project camel by apache.
the class RedeliveryErrorHandler method prepareExchangeAfterFailureNotHandled.
private void prepareExchangeAfterFailureNotHandled(Exchange exchange) {
log.trace("This exchange is not handled or continued so its marked as failed: {}", exchange);
// exception not handled, put exception back in the exchange
exchange.setProperty(Exchange.ERRORHANDLER_HANDLED, Boolean.FALSE);
exchange.setException(exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class));
// and put failure endpoint back as well
exchange.setProperty(Exchange.FAILURE_ENDPOINT, exchange.getProperty(Exchange.TO_ENDPOINT));
// and store the route id so we know in which route we failed
UnitOfWork uow = exchange.getUnitOfWork();
if (uow != null && uow.getRouteContext() != null) {
exchange.setProperty(Exchange.FAILURE_ROUTE_ID, uow.getRouteContext().getRoute().getId());
}
}
use of org.apache.camel.spi.UnitOfWork in project camel by apache.
the class AsyncProcessorHelper method process.
/**
* Calls the async version of the processor's process method.
* <p/>
* This implementation supports transacted {@link Exchange}s which ensure those are run in a synchronous fashion.
* See more details at {@link org.apache.camel.AsyncProcessor}.
*
* @param processor the processor
* @param exchange the exchange
* @param callback the callback
* @return <tt>true</tt> to continue execute synchronously, <tt>false</tt> to continue being executed asynchronously
* @deprecated should no longer be needed, instead invoke the process method on the {@link AsyncProcessor} directly,
* instead of using this method.
*/
@Deprecated
public static boolean process(final AsyncProcessor processor, final Exchange exchange, final AsyncCallback callback) {
boolean sync;
if (exchange.isTransacted()) {
// must be synchronized for transacted exchanges
LOG.trace("Transacted Exchange must be routed synchronously for exchangeId: {} -> {}", exchange.getExchangeId(), exchange);
try {
process(processor, exchange);
} catch (Throwable e) {
exchange.setException(e);
}
callback.done(true);
sync = 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);
}
// we support asynchronous routing so invoke it
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;
}
Aggregations