Search in sources :

Example 1 with ModelCamelContext

use of org.apache.camel.model.ModelCamelContext 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 ModelCamelContext

use of org.apache.camel.model.ModelCamelContext in project camel by apache.

the class RouteBuilder method getContext.

// Properties
// -----------------------------------------------------------------------
public ModelCamelContext getContext() {
    ModelCamelContext context = super.getContext();
    if (context == null) {
        context = createContainer();
        setContext(context);
    }
    return context;
}
Also used : ModelCamelContext(org.apache.camel.model.ModelCamelContext)

Example 3 with ModelCamelContext

use of org.apache.camel.model.ModelCamelContext in project camel by apache.

the class DefaultManagementObjectStrategy method getManagedObjectForRoute.

public Object getManagedObjectForRoute(CamelContext context, Route route) {
    ManagedRoute mr;
    if (route.supportsSuspension()) {
        mr = new ManagedSuspendableRoute((ModelCamelContext) context, route);
    } else {
        mr = new ManagedRoute((ModelCamelContext) context, route);
    }
    mr.init(context.getManagementStrategy());
    return mr;
}
Also used : ManagedRoute(org.apache.camel.management.mbean.ManagedRoute) ManagedSuspendableRoute(org.apache.camel.management.mbean.ManagedSuspendableRoute) ModelCamelContext(org.apache.camel.model.ModelCamelContext)

Example 4 with ModelCamelContext

use of org.apache.camel.model.ModelCamelContext in project camel by apache.

the class SpringScheduledRoutePolicyTest method startRouteWithPolicy.

@SuppressWarnings("unchecked")
private CamelContext startRouteWithPolicy(String policyBeanName) throws Exception {
    CamelContext context = new DefaultCamelContext();
    List<RouteDefinition> routes = (List<RouteDefinition>) applicationContext.getBean("testRouteContext");
    RoutePolicy policy = applicationContext.getBean(policyBeanName, RoutePolicy.class);
    assertTrue(getTestType() == TestType.SIMPLE ? policy instanceof SimpleScheduledRoutePolicy : policy instanceof CronScheduledRoutePolicy);
    routes.get(0).routePolicy(policy);
    ((ModelCamelContext) context).addRouteDefinitions(routes);
    context.start();
    return context;
}
Also used : CamelContext(org.apache.camel.CamelContext) ModelCamelContext(org.apache.camel.model.ModelCamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) RouteDefinition(org.apache.camel.model.RouteDefinition) List(java.util.List) RoutePolicy(org.apache.camel.spi.RoutePolicy) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ModelCamelContext(org.apache.camel.model.ModelCamelContext)

Example 5 with ModelCamelContext

use of org.apache.camel.model.ModelCamelContext in project camel by apache.

the class CamelTestSupport method doSetUp.

private void doSetUp() throws Exception {
    log.debug("setUp test");
    if (!useJmx()) {
        disableJMX();
    } else {
        enableJMX();
    }
    CamelContext c2 = createCamelContext();
    if (c2 instanceof ModelCamelContext) {
        context = (ModelCamelContext) c2;
    } else {
        throw new Exception("Context must be a ModelCamelContext");
    }
    threadCamelContext.set(context);
    assertNotNull(context, "No context found!");
    // reduce default shutdown timeout to avoid waiting for 300 seconds
    context.getShutdownStrategy().setTimeout(getShutdownTimeout());
    // set debugger if enabled
    if (isUseDebugger()) {
        context.setDebugger(new DefaultDebugger());
        context.getDebugger().addBreakpoint(breakpoint);
    // note: when stopping CamelContext it will automatic remove the breakpoint
    }
    template = context.createProducerTemplate();
    template.start();
    consumer = context.createConsumerTemplate();
    consumer.start();
    threadTemplate.set(template);
    threadConsumer.set(consumer);
    // enable auto mocking if enabled
    String pattern = isMockEndpoints();
    if (pattern != null) {
        context.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(pattern));
    }
    pattern = isMockEndpointsAndSkip();
    if (pattern != null) {
        context.addRegisterEndpointCallback(new InterceptSendToMockEndpointStrategy(pattern, true));
    }
    // configure properties component (mandatory for testing)
    context.getComponent("properties", PropertiesComponent.class);
    postProcessTest();
    if (isUseRouteBuilder()) {
        RoutesBuilder[] builders = createRouteBuilders();
        for (RoutesBuilder builder : builders) {
            log.debug("Using created route builder: " + builder);
            context.addRoutes(builder);
        }
        boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
        if (skip) {
            log.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true.");
        } else if (isUseAdviceWith()) {
            log.info("Skipping starting CamelContext as isUseAdviceWith is set to true.");
        } else {
            startCamelContext();
        }
    } else {
        log.debug("Using route builder from the created context: " + context);
    }
    log.debug("Routing Rules are: " + context.getRoutes());
    assertValidContext(context);
    INIT.set(true);
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) ModelCamelContext(org.apache.camel.model.ModelCamelContext) DefaultDebugger(org.apache.camel.impl.DefaultDebugger) InterceptSendToMockEndpointStrategy(org.apache.camel.impl.InterceptSendToMockEndpointStrategy) NoSuchEndpointException(org.apache.camel.NoSuchEndpointException) RoutesBuilder(org.apache.camel.RoutesBuilder) ModelCamelContext(org.apache.camel.model.ModelCamelContext)

Aggregations

ModelCamelContext (org.apache.camel.model.ModelCamelContext)14 RouteDefinition (org.apache.camel.model.RouteDefinition)5 CamelContext (org.apache.camel.CamelContext)4 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)4 List (java.util.List)2 FromDefinition (org.apache.camel.model.FromDefinition)2 RoutePolicy (org.apache.camel.spi.RoutePolicy)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ErrorHandlerFactory (org.apache.camel.ErrorHandlerFactory)1 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)1 RoutesBuilder (org.apache.camel.RoutesBuilder)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 TimerComponent (org.apache.camel.component.timer.TimerComponent)1 DefaultDebugger (org.apache.camel.impl.DefaultDebugger)1 InterceptSendToMockEndpointStrategy (org.apache.camel.impl.InterceptSendToMockEndpointStrategy)1 ManagedRoute (org.apache.camel.management.mbean.ManagedRoute)1 ManagedSuspendableRoute (org.apache.camel.management.mbean.ManagedSuspendableRoute)1 InterceptFromDefinition (org.apache.camel.model.InterceptFromDefinition)1