Search in sources :

Example 1 with ErrorHandlerFactory

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;
}
Also used : ErrorHandlerFactory(org.apache.camel.ErrorHandlerFactory) ModelCamelContext(org.apache.camel.model.ModelCamelContext)

Example 2 with ErrorHandlerFactory

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;
}
Also used : ErrorHandlerFactory(org.apache.camel.ErrorHandlerFactory)

Example 3 with ErrorHandlerFactory

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;
}
Also used : UnitOfWork(org.apache.camel.spi.UnitOfWork) AsyncProcessor(org.apache.camel.AsyncProcessor) Processor(org.apache.camel.Processor) ErrorHandlerFactory(org.apache.camel.ErrorHandlerFactory) AtomicException(org.apache.camel.util.concurrent.AtomicException) ExecutionException(java.util.concurrent.ExecutionException) CamelExchangeException(org.apache.camel.CamelExchangeException)

Example 4 with ErrorHandlerFactory

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());
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) ErrorHandlerFactory(org.apache.camel.ErrorHandlerFactory)

Example 5 with ErrorHandlerFactory

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;
}
Also used : FailedToCreateRouteException(org.apache.camel.FailedToCreateRouteException) ArrayList(java.util.ArrayList) ErrorHandlerFactory(org.apache.camel.ErrorHandlerFactory) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) FailedToCreateRouteException(org.apache.camel.FailedToCreateRouteException) DefaultRouteContext(org.apache.camel.impl.DefaultRouteContext) RouteContext(org.apache.camel.spi.RouteContext)

Aggregations

ErrorHandlerFactory (org.apache.camel.ErrorHandlerFactory)10 AsyncProcessor (org.apache.camel.AsyncProcessor)2 Processor (org.apache.camel.Processor)2 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 CamelExchangeException (org.apache.camel.CamelExchangeException)1 FailedToCreateProducerException (org.apache.camel.FailedToCreateProducerException)1 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)1 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)1 ErrorHandlerBuilderRef (org.apache.camel.builder.ErrorHandlerBuilderRef)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 DefaultRouteContext (org.apache.camel.impl.DefaultRouteContext)1 ModelCamelContext (org.apache.camel.model.ModelCamelContext)1 InterceptEndpointProcessor (org.apache.camel.processor.InterceptEndpointProcessor)1 LifecycleStrategy (org.apache.camel.spi.LifecycleStrategy)1 RouteContext (org.apache.camel.spi.RouteContext)1 UnitOfWork (org.apache.camel.spi.UnitOfWork)1 AtomicException (org.apache.camel.util.concurrent.AtomicException)1