public void onErrorHandlerAdd(RouteContext routeContext, Processor errorHandler, ErrorHandlerFactory errorHandlerBuilder) { if (!shouldRegister(errorHandler, null)) { // avoid registering if not needed return; } Object me = getManagementObjectStrategy().getManagedObjectForErrorHandler(camelContext, routeContext, errorHandler, errorHandlerBuilder); // skip already managed services, for example if a route has been restarted if (getManagementStrategy().isManaged(me, null)) { LOG.trace("The error handler builder is already managed: {}", errorHandlerBuilder); return; } try { manageObject(me); } catch (Exception e) { LOG.warn("Could not register error handler builder: " + errorHandlerBuilder + " as ErrorHandler MBean.", e); } }
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; }
@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()); } }; }
protected RouteDefinition createRoute() { RouteDefinition route = new RouteDefinition(); ErrorHandlerFactory handler = getErrorHandlerBuilder(); if (handler != null) { route.setErrorHandlerBuilderIfNull(handler); } return route; }
/** * 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; }
/** * Installs the given <a href="http://camel.apache.org/error-handler.html">error handler</a> builder. * * @param errorHandlerBuilder the error handler to be used by default for all child routes * @return the current builder with the error handler configured */ public RouteDefinition errorHandler(ErrorHandlerFactory errorHandlerBuilder) { setErrorHandlerBuilder(errorHandlerBuilder); // we are now using a route scoped error handler contextScopedErrorHandler = false; return this; }
private ErrorHandlerFactory createErrorHandlerBuilder() { if (errorHandlerRef != null) { return new ErrorHandlerBuilderRef(errorHandlerRef); } // return a reference to the default error handler return new ErrorHandlerBuilderRef(ErrorHandlerBuilderRef.DEFAULT_ERROR_HANDLER_BUILDER); }
@XmlTransient public ErrorHandlerFactory getErrorHandlerBuilder() { if (errorHandlerBuilder == null) { errorHandlerBuilder = createErrorHandlerBuilder(); } return errorHandlerBuilder; }
@SuppressWarnings("deprecation") public boolean isContextScopedErrorHandler(CamelContext context) { if (!contextScopedErrorHandler) { return false; } // if error handler ref is configured it may refer to a context scoped, so we need to check this first // 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; }
public void onErrorHandlerRemove(RouteContext routeContext, Processor errorHandler, ErrorHandlerFactory errorHandlerBuilder) { if (!initialized) { return; } Object me = getManagementObjectStrategy().getManagedObjectForErrorHandler(camelContext, routeContext, errorHandler, errorHandlerBuilder); if (me != null) { try { unmanageObject(me); } catch (Exception e) { LOG.warn("Could not unregister error handler: " + me + " as ErrorHandler MBean.", e); } } }
Object getManagedObjectForErrorHandler(CamelContext context, RouteContext routeContext, Processor errorHandler, ErrorHandlerFactory errorHandlerBuilder);
public ErrorHandlerFactory getErrorHandlerBuilder() { return errorHandlerBuilder; }
public void setErrorHandlerBuilder(ErrorHandlerFactory errorHandlerBuilder) { this.errorHandlerBuilder = errorHandlerBuilder; }
/** * Sets the error handler if one is not already set */ public void setErrorHandlerBuilderIfNull(ErrorHandlerFactory errorHandlerBuilder) { if (this.errorHandlerBuilder == null) { setErrorHandlerBuilder(errorHandlerBuilder); } }
/** * Sets the error handler to use with processors created by this builder */ public void setErrorHandlerBuilder(ErrorHandlerFactory errorHandlerBuilder) { this.errorHandlerBuilder = errorHandlerBuilder; }
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(); // create error handler (create error handler directly to keep it light weight, // 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; }
@Override public void onErrorHandlerAdd(RouteContext routeContext, Processor errorHandler, ErrorHandlerFactory errorHandlerBuilder) { // noop }
@Override public void onErrorHandlerRemove(RouteContext routeContext, Processor errorHandler, ErrorHandlerFactory errorHandlerBuilder) { // noop }
public Object getManagedObjectForErrorHandler(CamelContext context, RouteContext routeContext, Processor errorHandler, ErrorHandlerFactory errorHandlerBuilder) { ManagedErrorHandler me = new ManagedErrorHandler(routeContext, errorHandler, errorHandlerBuilder); me.init(context.getManagementStrategy()); return me; }
public ManagedErrorHandler(RouteContext routeContext, Processor errorHandler, ErrorHandlerFactory builder) { this.routeContext = routeContext; this.errorHandler = errorHandler; this.errorHandlerBuilder = builder; }
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; } // must do a 2nd lookup in case this is also a reference // (this happens with spring DSL using errorHandlerRef on <route> as it gets a bit // 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); }
/** * 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; // if the ref is the default then we do not have any explicit error handler configured // if that is the case then use error handlers configured on the route, as for instance // 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; }
public void onErrorHandlerAdd(RouteContext routeContext, Processor errorHandler, ErrorHandlerFactory errorHandlerBuilder) { events.add("onErrorHandlerAdd"); }
public void onErrorHandlerRemove(RouteContext routeContext, Processor errorHandler, ErrorHandlerFactory errorHandlerBuilder) { events.add("onErrorHandlerRemove"); }
@Override @Inject(optional = true) public void setErrorHandlerBuilder(ErrorHandlerFactory errorHandlerBuilder) { super.setErrorHandlerBuilder(errorHandlerBuilder); }
@Override public void onErrorHandlerAdd(RouteContext routeContext, Processor processor, ErrorHandlerFactory errorHandlerFactory) { }
@Override public void onErrorHandlerRemove(RouteContext routeContext, Processor processor, ErrorHandlerFactory errorHandlerFactory) { }
@Override public void setErrorHandlerBuilder(ErrorHandlerFactory errorHandlerBuilder) { context.setErrorHandlerBuilder(errorHandlerBuilder); }
/** * Notification on adding error handler. * * @param routeContext the added route context * @param errorHandler the error handler * @param errorHandlerBuilder the error handler builder */ void onErrorHandlerAdd(RouteContext routeContext, Processor errorHandler, ErrorHandlerFactory errorHandlerBuilder);
/** * Notification on removing error handler. * * @param routeContext the removed route context * @param errorHandler the error handler * @param errorHandlerBuilder the error handler builder */ void onErrorHandlerRemove(RouteContext routeContext, Processor errorHandler, ErrorHandlerFactory errorHandlerBuilder);
/** * Lookup the error handler by the given ref * * @param routeContext the route context * @param ref reference id for the error handler * @return the error handler */ public static ErrorHandlerFactory lookupErrorHandlerBuilder(RouteContext routeContext, String ref) { return lookupErrorHandlerBuilder(routeContext, ref, true); }
ObjectName getObjectNameForErrorHandler(RouteContext routeContext, Processor errorHandler, ErrorHandlerFactory builder) throws MalformedObjectNameException;