Search in sources :

Example 1 with RoutesBuilder

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

the class RoutesCollector method onApplicationEvent.

// Overridden
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    ApplicationContext applicationContext = event.getApplicationContext();
    // only listen to context refresh of "my" applicationContext
    if (this.applicationContext.equals(applicationContext)) {
        CamelContext camelContext = event.getApplicationContext().getBean(CamelContext.class);
        // only add and start Camel if its stopped (initial state)
        if (camelContext.getStatus().isStopped()) {
            LOG.debug("Post-processing CamelContext bean: {}", camelContext.getName());
            for (RoutesBuilder routesBuilder : configuration.routes()) {
                // filter out abstract classes
                boolean abs = Modifier.isAbstract(routesBuilder.getClass().getModifiers());
                if (!abs) {
                    try {
                        LOG.debug("Injecting following route into the CamelContext: {}", routesBuilder);
                        camelContext.addRoutes(routesBuilder);
                    } catch (Exception e) {
                        throw new CamelSpringJavaconfigInitializationException(e);
                    }
                }
            }
            try {
                boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
                if (skip) {
                    LOG.info("Skipping starting CamelContext(s) as system property skipStartingCamelContext is set to be true.");
                } else {
                    // start camel
                    camelContext.start();
                }
            } catch (Exception e) {
                throw new CamelSpringJavaconfigInitializationException(e);
            }
        }
    } else {
        LOG.debug("Ignore ContextRefreshedEvent: {}", event);
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) ApplicationContext(org.springframework.context.ApplicationContext) RoutesBuilder(org.apache.camel.RoutesBuilder)

Example 2 with RoutesBuilder

use of org.apache.camel.RoutesBuilder 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)

Example 3 with RoutesBuilder

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

the class PackageScanRouteBuilderFinder method appendBuilders.

/**
     * Appends all the {@link org.apache.camel.builder.RouteBuilder} instances that can be found on the classpath
     */
public void appendBuilders(List<RoutesBuilder> list) throws IllegalAccessException, InstantiationException {
    Set<Class<?>> classes = resolver.findImplementations(RoutesBuilder.class, packages);
    for (Class<?> aClass : classes) {
        LOG.trace("Found RouteBuilder class: {}", aClass);
        // certain beans should be ignored
        if (shouldIgnoreBean(aClass)) {
            LOG.debug("Ignoring RouteBuilder class: {}", aClass);
            continue;
        }
        if (!isValidClass(aClass)) {
            LOG.debug("Ignoring invalid RouteBuilder class: {}", aClass);
            continue;
        }
        // type is valid so create and instantiate the builder
        @SuppressWarnings("unchecked") RoutesBuilder builder = instantiateBuilder((Class<? extends RoutesBuilder>) aClass);
        if (beanPostProcessor != null) {
            // Inject the annotated resource
            beanPostProcessor.postProcessBeforeInitialization(builder, builder.toString());
        }
        LOG.debug("Adding instantiated RouteBuilder: {}", builder);
        list.add(builder);
    }
}
Also used : RoutesBuilder(org.apache.camel.RoutesBuilder)

Example 4 with RoutesBuilder

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

the class PackageScanRouteBuilderFinder method appendBuilders.

/**
     * Appends all the {@link org.apache.camel.builder.RouteBuilder} instances that can be found on the classpath
     */
public void appendBuilders(List<RoutesBuilder> list) throws IllegalAccessException, InstantiationException {
    Set<Class<?>> classes = resolver.findImplementations(RoutesBuilder.class, packages);
    for (Class<?> aClass : classes) {
        LOG.trace("Found RouteBuilder class: {}", aClass);
        // certain beans should be ignored
        if (shouldIgnoreBean(aClass)) {
            LOG.debug("Ignoring RouteBuilder class: {}", aClass);
            continue;
        }
        if (!isValidClass(aClass)) {
            LOG.debug("Ignoring invalid RouteBuilder class: {}", aClass);
            continue;
        }
        // type is valid so create and instantiate the builder
        RoutesBuilder builder = instantiateBuilder(aClass);
        LOG.debug("Adding instantiated RouteBuilder: {}", builder);
        list.add(builder);
    }
}
Also used : RoutesBuilder(org.apache.camel.RoutesBuilder)

Example 5 with RoutesBuilder

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

the class PackageScanRouteBuilderFinder method appendBuilders.

/**
     * Appends all the {@link org.apache.camel.builder.RouteBuilder} instances that can be found on the classpath
     */
void appendBuilders(List<RoutesBuilder> list) throws IllegalAccessException, InstantiationException {
    Set<Class<?>> classes = resolver.findImplementations(RoutesBuilder.class, packages);
    for (Class<?> aClass : classes) {
        logger.trace("Found RouteBuilder class: {}", aClass);
        // certain beans should be ignored
        if (shouldIgnoreBean(aClass)) {
            logger.debug("Ignoring RouteBuilder class: {}", aClass);
            continue;
        }
        if (!isValidClass(aClass)) {
            logger.debug("Ignoring invalid RouteBuilder class: {}", aClass);
            continue;
        }
        // type is valid so create and instantiate the builder
        @SuppressWarnings("unchecked") RoutesBuilder builder = instantiateBuilder((Class<? extends RoutesBuilder>) aClass);
        logger.debug("Adding instantiated RouteBuilder: {}", builder);
        list.add(builder);
    }
}
Also used : RoutesBuilder(org.apache.camel.RoutesBuilder)

Aggregations

RoutesBuilder (org.apache.camel.RoutesBuilder)10 CamelContext (org.apache.camel.CamelContext)3 DefaultDebugger (org.apache.camel.impl.DefaultDebugger)2 InterceptSendToMockEndpointStrategy (org.apache.camel.impl.InterceptSendToMockEndpointStrategy)2 ApplicationContext (org.springframework.context.ApplicationContext)2 Provides (com.google.inject.Provides)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 CamelException (org.apache.camel.CamelException)1 NoSuchEndpointException (org.apache.camel.NoSuchEndpointException)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 PropertiesComponent (org.apache.camel.component.properties.PropertiesComponent)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 MainDurationEventNotifier (org.apache.camel.main.MainDurationEventNotifier)1 ModelCamelContext (org.apache.camel.model.ModelCamelContext)1 RouteBuilderDefinition (org.apache.camel.model.RouteBuilderDefinition)1 EventNotifier (org.apache.camel.spi.EventNotifier)1