Search in sources :

Example 11 with ModelCamelContext

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

the class ContextTestSupport method setUp.

@Override
protected void setUp() throws Exception {
    // make SEDA testing faster
    System.setProperty("CamelSedaPollTimeout", "10");
    if (!useJmx()) {
        disableJMX();
    } else {
        enableJMX();
    }
    CamelContext c2 = createCamelContext();
    if (c2 instanceof ModelCamelContext) {
        context = (ModelCamelContext) c2;
    } else {
        throw new Exception("Context must be a ModelCamelContext");
    }
    assertValidContext(context);
    // reduce default shutdown timeout to avoid waiting for 300 seconds
    context.getShutdownStrategy().setTimeout(10);
    template = context.createProducerTemplate();
    template.start();
    consumer = context.createConsumerTemplate();
    consumer.start();
    // create a default notifier when 1 exchange is done which is the most common case
    oneExchangeDone = event().whenDone(1).create();
    if (isUseRouteBuilder()) {
        RouteBuilder[] builders = createRouteBuilders();
        for (RouteBuilder builder : builders) {
            log.debug("Using created route builder: " + builder);
            context.addRoutes(builder);
        }
        startCamelContext();
    } else {
        log.debug("isUseRouteBuilder() is false");
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) ModelCamelContext(org.apache.camel.model.ModelCamelContext) RouteBuilder(org.apache.camel.builder.RouteBuilder) ModelCamelContext(org.apache.camel.model.ModelCamelContext)

Example 12 with ModelCamelContext

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

the class RouteBuilder method populateRests.

protected void populateRests() throws Exception {
    ModelCamelContext camelContext = getContext();
    if (camelContext == null) {
        throw new IllegalArgumentException("CamelContext has not been injected!");
    }
    getRestCollection().setCamelContext(camelContext);
    // setup rest configuration before adding the rests
    if (getRestConfigurations() != null) {
        for (Map.Entry<String, RestConfigurationDefinition> entry : getRestConfigurations().entrySet()) {
            RestConfiguration config = entry.getValue().asRestConfiguration(getContext());
            if ("".equals(entry.getKey())) {
                camelContext.setRestConfiguration(config);
            } else {
                camelContext.addRestConfiguration(config);
            }
        }
    }
    camelContext.addRestDefinitions(getRestCollection().getRests());
    // convert rests into routes so we they are routes for runtime
    List<RouteDefinition> routes = new ArrayList<RouteDefinition>();
    for (RestDefinition rest : getRestCollection().getRests()) {
        List<RouteDefinition> list = rest.asRouteDefinition(getContext());
        routes.addAll(list);
    }
    // convert rests api-doc into routes so they are routes for runtime
    for (RestConfiguration config : camelContext.getRestConfigurations()) {
        if (config.getApiContextPath() != null) {
            // avoid adding rest-api multiple times, in case multiple RouteBuilder classes is added
            // to the CamelContext, as we only want to setup rest-api once
            // so we check all existing routes if they have rest-api route already added
            boolean hasRestApi = false;
            for (RouteDefinition route : camelContext.getRouteDefinitions()) {
                FromDefinition from = route.getInputs().get(0);
                if (from.getUri() != null && from.getUri().startsWith("rest-api:")) {
                    hasRestApi = true;
                }
            }
            if (!hasRestApi) {
                RouteDefinition route = RestDefinition.asRouteApiDefinition(camelContext, config);
                log.debug("Adding routeId: {} as rest-api route", route.getId());
                routes.add(route);
            }
        }
    }
    // add the rest routes
    for (RouteDefinition route : routes) {
        getRouteCollection().route(route);
    }
}
Also used : RestDefinition(org.apache.camel.model.rest.RestDefinition) FromDefinition(org.apache.camel.model.FromDefinition) InterceptFromDefinition(org.apache.camel.model.InterceptFromDefinition) ArrayList(java.util.ArrayList) ModelCamelContext(org.apache.camel.model.ModelCamelContext) RestConfigurationDefinition(org.apache.camel.model.rest.RestConfigurationDefinition) RouteDefinition(org.apache.camel.model.RouteDefinition) RestConfiguration(org.apache.camel.spi.RestConfiguration) HashMap(java.util.HashMap) Map(java.util.Map)

Example 13 with ModelCamelContext

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

the class RouteBuilder method populateRoutes.

protected void populateRoutes() throws Exception {
    ModelCamelContext camelContext = getContext();
    if (camelContext == null) {
        throw new IllegalArgumentException("CamelContext has not been injected!");
    }
    getRouteCollection().setCamelContext(camelContext);
    camelContext.addRouteDefinitions(getRouteCollection().getRoutes());
}
Also used : ModelCamelContext(org.apache.camel.model.ModelCamelContext)

Example 14 with ModelCamelContext

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

the class RouteBuilder method checkInitialized.

// Implementation methods
// -----------------------------------------------------------------------
@SuppressWarnings("deprecation")
protected void checkInitialized() throws Exception {
    if (initialized.compareAndSet(false, true)) {
        // Set the CamelContext ErrorHandler here
        ModelCamelContext camelContext = getContext();
        if (camelContext.getErrorHandlerBuilder() != null) {
            setErrorHandlerBuilder(camelContext.getErrorHandlerBuilder());
        }
        configure();
        // a route builder prepares the route definitions correctly already
        for (RouteDefinition route : getRouteCollection().getRoutes()) {
            route.markPrepared();
        }
    }
}
Also used : RouteDefinition(org.apache.camel.model.RouteDefinition) 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