use of org.apache.camel.ErrorHandlerFactory in project camel by apache.
the class ErrorHandlerBuilderRef method lookupErrorHandlerBuilder.
/**
* Lookup the error handler by the given ref
*
* @param routeContext the route context
* @param ref reference id for the error handler
* @param mandatory whether the error handler must exists, if not a {@link org.apache.camel.NoSuchBeanException} is thrown
* @return the error handler
*/
public static ErrorHandlerFactory lookupErrorHandlerBuilder(RouteContext routeContext, String ref, boolean mandatory) {
ErrorHandlerFactory answer;
// the transacted error handler could have been configured on the route so we should use that one
if (!isErrorHandlerBuilderConfigured(ref)) {
// see if there has been configured a route builder on the route
answer = routeContext.getRoute().getErrorHandlerBuilder();
if (answer == null && routeContext.getRoute().getErrorHandlerRef() != null) {
answer = routeContext.lookup(routeContext.getRoute().getErrorHandlerRef(), ErrorHandlerBuilder.class);
}
if (answer == null) {
// fallback to the default error handler if none configured on the route
answer = new DefaultErrorHandlerBuilder();
}
// check if its also a ref with no error handler configuration like me
if (answer instanceof ErrorHandlerBuilderRef) {
ErrorHandlerBuilderRef other = (ErrorHandlerBuilderRef) answer;
String otherRef = other.getRef();
if (!isErrorHandlerBuilderConfigured(otherRef)) {
// the other has also no explicit error handler configured then fallback to the handler
// configured on the parent camel context
answer = lookupErrorHandlerBuilder((ModelCamelContext) routeContext.getCamelContext());
}
if (answer == null) {
// the other has also no explicit error handler configured then fallback to the default error handler
// otherwise we could recursive loop forever (triggered by createErrorHandler method)
answer = new DefaultErrorHandlerBuilder();
}
// inherit the error handlers from the other as they are to be shared
// this is needed by camel-spring when none error handler has been explicit configured
((ErrorHandlerBuilder) answer).setErrorHandlers(routeContext, other.getErrorHandlers(routeContext));
}
} else {
// use specific configured error handler
if (mandatory) {
answer = routeContext.mandatoryLookup(ref, ErrorHandlerBuilder.class);
} else {
answer = routeContext.lookup(ref, ErrorHandlerBuilder.class);
}
}
return answer;
}
use of org.apache.camel.ErrorHandlerFactory in project camel by apache.
the class ErrorHandlerBuilderRef method lookupErrorHandlerBuilder.
protected static ErrorHandlerFactory lookupErrorHandlerBuilder(ModelCamelContext camelContext) {
@SuppressWarnings("deprecation") ErrorHandlerFactory answer = camelContext.getErrorHandlerBuilder();
if (answer instanceof ErrorHandlerBuilderRef) {
ErrorHandlerBuilderRef other = (ErrorHandlerBuilderRef) answer;
String otherRef = other.getRef();
if (isErrorHandlerBuilderConfigured(otherRef)) {
answer = camelContext.getRegistry().lookupByNameAndType(otherRef, ErrorHandlerBuilder.class);
if (answer == null) {
throw new IllegalArgumentException("ErrorHandlerBuilder with id " + otherRef + " not found in registry.");
}
}
}
return answer;
}
use of org.apache.camel.ErrorHandlerFactory in project camel by apache.
the class MulticastProcessor method createErrorHandler.
protected Processor createErrorHandler(RouteContext routeContext, Exchange exchange, Processor processor) {
Processor answer;
boolean tryBlock = exchange.getProperty(Exchange.TRY_ROUTE_BLOCK, false, boolean.class);
// do not wrap in error handler if we are inside a try block
if (!tryBlock && routeContext != null) {
// wrap the producer in error handler so we have fine grained error handling on
// the output side instead of the input side
// this is needed to support redelivery on that output alone and not doing redelivery
// for the entire multicast block again which will start from scratch again
// create key for cache
final PreparedErrorHandler key = new PreparedErrorHandler(routeContext, processor);
// lookup cached first to reuse and preserve memory
answer = errorHandlers.get(key);
if (answer != null) {
LOG.trace("Using existing error handler for: {}", processor);
return answer;
}
LOG.trace("Creating error handler for: {}", processor);
ErrorHandlerFactory builder = routeContext.getRoute().getErrorHandlerBuilder();
// instead of using ProcessorDefinition.wrapInErrorHandler)
try {
processor = builder.createErrorHandler(routeContext, processor);
// and wrap in unit of work processor so the copy exchange also can run under UoW
answer = createUnitOfWorkProcessor(routeContext, processor, exchange);
boolean child = exchange.getProperty(Exchange.PARENT_UNIT_OF_WORK, UnitOfWork.class) != null;
// must start the error handler
ServiceHelper.startServices(answer);
// here we don't cache the child unit of work
if (!child) {
// add to cache
errorHandlers.putIfAbsent(key, answer);
}
} catch (Exception e) {
throw ObjectHelper.wrapRuntimeCamelException(e);
}
} else {
// and wrap in unit of work processor so the copy exchange also can run under UoW
answer = createUnitOfWorkProcessor(routeContext, processor, exchange);
}
return answer;
}
use of org.apache.camel.ErrorHandlerFactory in project camel by apache.
the class DeadLetterChannelUseOriginalInBodyTest method createRouteBuilder.
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
// will use original
ErrorHandlerFactory a = deadLetterChannel("mock:a").maximumRedeliveries(2).redeliveryDelay(0).logStackTrace(false).useOriginalMessage();
// will NOT use original
ErrorHandlerFactory b = deadLetterChannel("mock:b").maximumRedeliveries(2).redeliveryDelay(0).logStackTrace(false);
from("direct:a").errorHandler(a).setBody(body().append(" World")).process(new MyThrowProcessor());
from("direct:b").errorHandler(b).setBody(body().append(" World")).process(new MyThrowProcessor());
}
};
}
use of org.apache.camel.ErrorHandlerFactory in project camel by apache.
the class RouteDefinition method addRoutes.
public List<RouteContext> addRoutes(ModelCamelContext camelContext, Collection<Route> routes) throws Exception {
List<RouteContext> answer = new ArrayList<RouteContext>();
@SuppressWarnings("deprecation") ErrorHandlerFactory handler = camelContext.getErrorHandlerBuilder();
if (handler != null) {
setErrorHandlerBuilderIfNull(handler);
}
for (FromDefinition fromType : inputs) {
RouteContext routeContext;
try {
routeContext = addRoutes(camelContext, routes, fromType);
} catch (FailedToCreateRouteException e) {
throw e;
} catch (Exception e) {
// wrap in exception which provide more details about which route was failing
throw new FailedToCreateRouteException(getId(), toString(), e);
}
answer.add(routeContext);
}
return answer;
}
Aggregations