Search in sources :

Example 6 with ErrorHandlerFactory

use of org.apache.camel.ErrorHandlerFactory in project camel by apache.

the class ProcessorDefinition method wrapInErrorHandler.

/**
     * Wraps the given output in an error handler
     *
     * @param routeContext the route context
     * @param output the output
     * @return the output wrapped with the error handler
     * @throws Exception can be thrown if failed to create error handler builder
     */
protected Processor wrapInErrorHandler(RouteContext routeContext, Processor output) throws Exception {
    ErrorHandlerFactory builder = routeContext.getRoute().getErrorHandlerBuilder();
    // create error handler
    Processor errorHandler = builder.createErrorHandler(routeContext, output);
    // invoke lifecycles so we can manage this error handler builder
    for (LifecycleStrategy strategy : routeContext.getCamelContext().getLifecycleStrategies()) {
        strategy.onErrorHandlerAdd(routeContext, errorHandler, builder);
    }
    return errorHandler;
}
Also used : InterceptEndpointProcessor(org.apache.camel.processor.InterceptEndpointProcessor) Processor(org.apache.camel.Processor) LifecycleStrategy(org.apache.camel.spi.LifecycleStrategy) ErrorHandlerFactory(org.apache.camel.ErrorHandlerFactory)

Example 7 with ErrorHandlerFactory

use of org.apache.camel.ErrorHandlerFactory in project camel by apache.

the class RoutingSlip method createErrorHandler.

protected AsyncProcessor createErrorHandler(RouteContext routeContext, Exchange exchange, AsyncProcessor processor, Endpoint endpoint) {
    AsyncProcessor answer = processor;
    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 routingslip/dynamic-router block again which will start from scratch again
        log.trace("Creating error handler for: {}", processor);
        ErrorHandlerFactory builder = routeContext.getRoute().getErrorHandlerBuilder();
        // instead of using ProcessorDefinition.wrapInErrorHandler)
        try {
            answer = (AsyncProcessor) builder.createErrorHandler(routeContext, processor);
            // must start the error handler
            ServiceHelper.startServices(answer);
        } catch (Exception e) {
            throw ObjectHelper.wrapRuntimeCamelException(e);
        }
    }
    return answer;
}
Also used : AsyncProcessor(org.apache.camel.AsyncProcessor) ErrorHandlerFactory(org.apache.camel.ErrorHandlerFactory) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException)

Example 8 with ErrorHandlerFactory

use of org.apache.camel.ErrorHandlerFactory in project camel by apache.

the class DefaultManagementNamingStrategy method getObjectNameForErrorHandler.

public ObjectName getObjectNameForErrorHandler(RouteContext routeContext, Processor errorHandler, ErrorHandlerFactory builder) throws MalformedObjectNameException {
    StringBuilder buffer = new StringBuilder();
    buffer.append(domainName).append(":");
    buffer.append(KEY_CONTEXT + "=").append(getContextId(routeContext.getCamelContext())).append(",");
    buffer.append(KEY_TYPE + "=").append(TYPE_ERRORHANDLER + ",");
    // we want to only register one instance of the various error handler types and thus do some lookup
    // if its a ErrorHandlerBuildRef. We need a bit of work to do that as there are potential indirection.
    String ref = null;
    if (builder instanceof ErrorHandlerBuilderRef) {
        ErrorHandlerBuilderRef builderRef = (ErrorHandlerBuilderRef) builder;
        // it has not then its an indirection and we should do some work to lookup the real builder
        ref = builderRef.getRef();
        ErrorHandlerFactory refBuilder = ErrorHandlerBuilderRef.lookupErrorHandlerBuilder(routeContext, builderRef.getRef(), false);
        if (refBuilder != null) {
            builder = refBuilder;
        }
        // complex with indirections for error handler references
        if (builder instanceof ErrorHandlerBuilderRef) {
            builderRef = (ErrorHandlerBuilderRef) builder;
            // does it refer to a non default error handler then do a 2nd lookup
            if (!builderRef.getRef().equals(ErrorHandlerBuilderRef.DEFAULT_ERROR_HANDLER_BUILDER)) {
                refBuilder = ErrorHandlerBuilderRef.lookupErrorHandlerBuilder(routeContext, builderRef.getRef(), false);
                if (refBuilder != null) {
                    ref = builderRef.getRef();
                    builder = refBuilder;
                }
            }
        }
    }
    if (ref != null) {
        String name = builder.getClass().getSimpleName() + "(ref:" + ref + ")";
        buffer.append(KEY_NAME + "=").append(ObjectName.quote(name));
    } else {
        // create a name based on its instance
        buffer.append(KEY_NAME + "=").append(builder.getClass().getSimpleName()).append("(").append(ObjectHelper.getIdentityHashCode(builder)).append(")");
    }
    return createObjectName(buffer);
}
Also used : ErrorHandlerFactory(org.apache.camel.ErrorHandlerFactory) ErrorHandlerBuilderRef(org.apache.camel.builder.ErrorHandlerBuilderRef)

Example 9 with ErrorHandlerFactory

use of org.apache.camel.ErrorHandlerFactory in project camel by apache.

the class RouteDefinition method isContextScopedErrorHandler.

@SuppressWarnings("deprecation")
public boolean isContextScopedErrorHandler(CamelContext context) {
    if (!contextScopedErrorHandler) {
        return false;
    }
    // the XML DSL will configure error handlers using refs, so we need this additional test
    if (errorHandlerRef != null) {
        ErrorHandlerFactory routeScoped = getErrorHandlerBuilder();
        ErrorHandlerFactory contextScoped = context.getErrorHandlerBuilder();
        return routeScoped != null && contextScoped != null && routeScoped == contextScoped;
    }
    return true;
}
Also used : ErrorHandlerFactory(org.apache.camel.ErrorHandlerFactory)

Example 10 with ErrorHandlerFactory

use of org.apache.camel.ErrorHandlerFactory in project camel by apache.

the class RoutesDefinition method createRoute.

// Implementation methods
//-------------------------------------------------------------------------
protected RouteDefinition createRoute() {
    RouteDefinition route = new RouteDefinition();
    ErrorHandlerFactory handler = getErrorHandlerBuilder();
    if (handler != null) {
        route.setErrorHandlerBuilderIfNull(handler);
    }
    return route;
}
Also used : ErrorHandlerFactory(org.apache.camel.ErrorHandlerFactory)

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