Search in sources :

Example 6 with ErrorHandlerBuilderRef

use of org.apache.camel.builder.ErrorHandlerBuilderRef in project camel by apache.

the class OnExceptionNotHandledErrorHandlerRefIssueTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            errorHandler(new ErrorHandlerBuilderRef("myDLC"));
            from("direct:start").onException(IllegalArgumentException.class).handled(false).to("mock:handled").end().to("mock:a").throwException(new IllegalArgumentException("Damn"));
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) ErrorHandlerBuilderRef(org.apache.camel.builder.ErrorHandlerBuilderRef)

Example 7 with ErrorHandlerBuilderRef

use of org.apache.camel.builder.ErrorHandlerBuilderRef in project camel by apache.

the class OnExceptionNotHandledErrorHandlerRefIssueTwoRoutesTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            errorHandler(new ErrorHandlerBuilderRef("myDLC"));
            from("direct:foo").to("mock:foo").throwException(new IllegalArgumentException("Damn Foo"));
            from("direct:start").onException(IllegalArgumentException.class).handled(false).to("mock:handled").end().to("mock:a").throwException(new IllegalArgumentException("Damn"));
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) ErrorHandlerBuilderRef(org.apache.camel.builder.ErrorHandlerBuilderRef)

Example 8 with ErrorHandlerBuilderRef

use of org.apache.camel.builder.ErrorHandlerBuilderRef in project camel by apache.

the class RouteDefinition method setErrorHandlerRef.

/**
     * Sets the bean ref name of the error handler builder to use on this route
     */
@XmlAttribute
public void setErrorHandlerRef(String errorHandlerRef) {
    this.errorHandlerRef = errorHandlerRef;
    // we use an specific error handler ref (from Spring DSL) then wrap that
    // with a error handler build ref so Camel knows its not just the default one
    setErrorHandlerBuilder(new ErrorHandlerBuilderRef(errorHandlerRef));
}
Also used : ErrorHandlerBuilderRef(org.apache.camel.builder.ErrorHandlerBuilderRef) XmlAttribute(javax.xml.bind.annotation.XmlAttribute)

Example 9 with ErrorHandlerBuilderRef

use of org.apache.camel.builder.ErrorHandlerBuilderRef 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 10 with ErrorHandlerBuilderRef

use of org.apache.camel.builder.ErrorHandlerBuilderRef 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;
}
Also used : ErrorHandlerBuilder(org.apache.camel.builder.ErrorHandlerBuilder) ErrorHandlerBuilderRef(org.apache.camel.builder.ErrorHandlerBuilderRef)

Aggregations

ErrorHandlerBuilderRef (org.apache.camel.builder.ErrorHandlerBuilderRef)17 RouteBuilder (org.apache.camel.builder.RouteBuilder)13 IOException (java.io.IOException)4 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 XmlAttribute (javax.xml.bind.annotation.XmlAttribute)1 ErrorHandlerFactory (org.apache.camel.ErrorHandlerFactory)1 ErrorHandlerBuilder (org.apache.camel.builder.ErrorHandlerBuilder)1