use of org.apache.camel.builder.ErrorHandlerBuilder in project camel by apache.
the class RouteDefinitionHelper method initParentAndErrorHandlerBuilder.
private static void initParentAndErrorHandlerBuilder(ModelCamelContext context, RouteDefinition route, List<ProcessorDefinition<?>> abstracts, List<OnExceptionDefinition> onExceptions) {
if (context != null) {
// let the route inherit the error handler builder from camel context if none already set
// must clone to avoid side effects while building routes using multiple RouteBuilders
ErrorHandlerBuilder builder = context.getErrorHandlerBuilder();
if (builder != null) {
builder = builder.cloneBuilder();
route.setErrorHandlerBuilderIfNull(builder);
}
}
// init parent and error handler builder on the route
initParentAndErrorHandlerBuilder(route);
// set the parent and error handler builder on the global on exceptions
if (onExceptions != null) {
for (OnExceptionDefinition global : onExceptions) {
initParentAndErrorHandlerBuilder(global);
}
}
}
use of org.apache.camel.builder.ErrorHandlerBuilder in project camel by apache.
the class SpringTransactionPolicy method wrap.
public Processor wrap(RouteContext routeContext, Processor processor) {
TransactionErrorHandler answer;
// the goal is to configure the error handler builder on the route as a transacted error handler,
// either its already a transacted or if not we replace it with a transacted one that we configure here
// and wrap the processor in the transacted error handler as we can have transacted routes that change
// propagation behavior, eg: from A required -> B -> requiresNew C (advanced use-case)
// if we should not support this we do not need to wrap the processor as we only need one transacted error handler
// find the existing error handler builder
ErrorHandlerBuilder builder = (ErrorHandlerBuilder) routeContext.getRoute().getErrorHandlerBuilder();
// check if its a ref if so then do a lookup
if (builder instanceof ErrorHandlerBuilderRef) {
// its a reference to a error handler so lookup the reference
ErrorHandlerBuilderRef builderRef = (ErrorHandlerBuilderRef) builder;
String ref = builderRef.getRef();
// and if so then we can safely replace that with our transacted error handler
if (ErrorHandlerBuilderRef.isErrorHandlerBuilderConfigured(ref)) {
LOG.debug("Looking up ErrorHandlerBuilder with ref: {}", ref);
builder = (ErrorHandlerBuilder) ErrorHandlerBuilderRef.lookupErrorHandlerBuilder(routeContext, ref);
}
}
if (builder != null && builder.supportTransacted()) {
// already a TX error handler then we are good to go
LOG.debug("The ErrorHandlerBuilder configured is already a TransactionErrorHandlerBuilder: {}", builder);
answer = createTransactionErrorHandler(routeContext, processor, builder);
answer.setExceptionPolicy(builder.getExceptionPolicyStrategy());
// configure our answer based on the existing error handler
builder.configure(routeContext, answer);
} else {
// routes anyway even though the error handler is not transactional, eg ease of configuration
if (builder != null) {
LOG.debug("The ErrorHandlerBuilder configured is not a TransactionErrorHandlerBuilder: {}", builder);
} else {
LOG.debug("No ErrorHandlerBuilder configured, will use default TransactionErrorHandlerBuilder settings");
}
TransactionErrorHandlerBuilder txBuilder = new TransactionErrorHandlerBuilder();
txBuilder.setTransactionTemplate(getTransactionTemplate());
txBuilder.setSpringTransactionPolicy(this);
if (builder != null) {
// use error handlers from the configured builder
txBuilder.setErrorHandlers(routeContext, builder.getErrorHandlers(routeContext));
}
answer = createTransactionErrorHandler(routeContext, processor, txBuilder);
answer.setExceptionPolicy(txBuilder.getExceptionPolicyStrategy());
// configure our answer based on the existing error handler
txBuilder.configure(routeContext, answer);
// set the route to use our transacted error handler builder
routeContext.getRoute().setErrorHandlerBuilder(txBuilder);
}
// return with wrapped transacted error handler
return answer;
}
use of org.apache.camel.builder.ErrorHandlerBuilder in project camel by apache.
the class OnExceptionDefinition method addRoutes.
public void addRoutes(RouteContext routeContext, Collection<Route> routes) throws Exception {
// and therefore is in a better position to decide among context/route scoped OnException at runtime
if (routeScoped == null) {
routeScoped = super.getParent() != null;
}
setHandledFromExpressionType(routeContext);
setContinuedFromExpressionType(routeContext);
setRetryWhileFromExpressionType(routeContext);
setOnRedeliveryFromRedeliveryRef(routeContext);
setOnExceptionOccurredFromOnExceptionOccurredRef(routeContext);
// load exception classes
if (exceptions != null && !exceptions.isEmpty()) {
exceptionClasses = createExceptionClasses(routeContext.getCamelContext().getClassResolver());
}
// must validate configuration before creating processor
validateConfiguration();
if (useOriginalMessagePolicy != null && useOriginalMessagePolicy) {
// ensure allow original is turned on
routeContext.setAllowUseOriginalMessage(true);
}
// lets attach this on exception to the route error handler
Processor child = createOutputsProcessor(routeContext);
if (child != null) {
// wrap in our special safe fallback error handler if OnException have child output
Processor errorHandler = new FatalFallbackErrorHandler(child);
String id = routeContext.getRoute().getId();
errorHandlers.put(id, errorHandler);
}
// lookup the error handler builder
ErrorHandlerBuilder builder = (ErrorHandlerBuilder) routeContext.getRoute().getErrorHandlerBuilder();
// and add this as error handlers
builder.addErrorHandlers(routeContext, this);
}
Aggregations