Search in sources :

Example 6 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");
    // jmx is enabled if we have configured to use it, or if dump route coverage is enabled (it requires JMX)
    boolean jmx = useJmx() || isDumpRouteCoverage();
    if (jmx) {
        enableJMX();
    } else {
        disableJMX();
    }
    context = (ModelCamelContext) createCamelContext();
    threadCamelContext.set(context);
    assertNotNull("No context found!", context);
    // reduce default shutdown timeout to avoid waiting for 300 seconds
    context.getShutdownStrategy().setTimeout(getShutdownTimeout());
    // set debugger if enabled
    if (isUseDebugger()) {
        if (context.getStatus().equals(ServiceStatus.Started)) {
            log.info("Cannot setting the Debugger to the starting CamelContext, stop the CamelContext now.");
            // we need to stop the context first to setup the debugger
            context.stop();
        }
        context.setDebugger(new DefaultDebugger());
        context.getDebugger().addBreakpoint(breakpoint);
    // note: when stopping CamelContext it will automatic remove the breakpoint
    }
    template = context.createProducerTemplate();
    template.start();
    fluentTemplate = context.createFluentProducerTemplate();
    fluentTemplate.start();
    consumer = context.createConsumerTemplate();
    consumer.start();
    threadTemplate.set(template);
    threadFluentTemplate.set(fluentTemplate);
    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)
    PropertiesComponent pc = context.getComponent("properties", PropertiesComponent.class);
    Properties extra = useOverridePropertiesWithPropertiesComponent();
    if (extra != null && !extra.isEmpty()) {
        pc.setOverrideProperties(extra);
    }
    Boolean ignore = ignoreMissingLocationWithPropertiesComponent();
    if (ignore != null) {
        pc.setIgnoreMissingLocation(ignore);
    }
    postProcessTest();
    if (isUseRouteBuilder()) {
        RoutesBuilder[] builders = createRouteBuilders();
        for (RoutesBuilder builder : builders) {
            log.debug("Using created route builder: " + builder);
            context.addRoutes(builder);
        }
        replaceFromEndpoints();
        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 {
        replaceFromEndpoints();
        log.debug("Using route builder from the created context: " + context);
    }
    log.debug("Routing Rules are: " + context.getRoutes());
    assertValidContext(context);
    INIT.set(true);
}
Also used : DefaultDebugger(org.apache.camel.impl.DefaultDebugger) InterceptSendToMockEndpointStrategy(org.apache.camel.impl.InterceptSendToMockEndpointStrategy) Properties(java.util.Properties) PropertiesComponent(org.apache.camel.component.properties.PropertiesComponent) RoutesBuilder(org.apache.camel.RoutesBuilder)

Example 7 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 : applicationContext.getBeansOfType(RoutesBuilder.class, configurationProperties.isIncludeNonSingletons(), true).values()) {
                // 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 CamelSpringBootInitializationException(e);
                    }
                }
            }
            try {
                boolean scan = !configurationProperties.getXmlRoutes().equals("false");
                if (scan) {
                    loadXmlRoutes(applicationContext, camelContext, configurationProperties.getXmlRoutes());
                }
                boolean scanRests = !configurationProperties.getXmlRests().equals("false");
                if (scanRests) {
                    loadXmlRests(applicationContext, camelContext, configurationProperties.getXmlRests());
                }
                for (CamelContextConfiguration camelContextConfiguration : camelContextConfigurations) {
                    LOG.debug("CamelContextConfiguration found. Invoking beforeApplicationStart: {}", camelContextConfiguration);
                    camelContextConfiguration.beforeApplicationStart(camelContext);
                }
                if (configurationProperties.isMainRunController()) {
                    CamelMainRunController controller = new CamelMainRunController(applicationContext, camelContext);
                    if (configurationProperties.getDurationMaxMessages() > 0 || configurationProperties.getDurationMaxIdleSeconds() > 0) {
                        if (configurationProperties.getDurationMaxMessages() > 0) {
                            LOG.info("CamelSpringBoot will terminate after processing {} messages", configurationProperties.getDurationMaxMessages());
                        }
                        if (configurationProperties.getDurationMaxIdleSeconds() > 0) {
                            LOG.info("CamelSpringBoot will terminate after being idle for more {} seconds", configurationProperties.getDurationMaxIdleSeconds());
                        }
                        // register lifecycle so we can trigger to shutdown the JVM when maximum number of messages has been processed
                        EventNotifier notifier = new MainDurationEventNotifier(camelContext, configurationProperties.getDurationMaxMessages(), configurationProperties.getDurationMaxIdleSeconds(), controller.getCompleted(), controller.getLatch(), true);
                        // register our event notifier
                        ServiceHelper.startService(notifier);
                        camelContext.getManagementStrategy().addEventNotifier(notifier);
                    }
                    if (configurationProperties.getDurationMaxSeconds() > 0) {
                        LOG.info("CamelSpringBoot will terminate after {} seconds", configurationProperties.getDurationMaxSeconds());
                        terminateMainControllerAfter(camelContext, configurationProperties.getDurationMaxSeconds(), controller.getCompleted(), controller.getLatch());
                    }
                    // controller will start Camel
                    LOG.info("Starting CamelMainRunController to ensure the main thread keeps running");
                    controller.start();
                } else {
                    if (applicationContext instanceof ConfigurableApplicationContext) {
                        ConfigurableApplicationContext cac = (ConfigurableApplicationContext) applicationContext;
                        if (configurationProperties.getDurationMaxSeconds() > 0) {
                            LOG.info("CamelSpringBoot will terminate after {} seconds", configurationProperties.getDurationMaxSeconds());
                            terminateApplicationContext(cac, camelContext, configurationProperties.getDurationMaxSeconds());
                        }
                        if (configurationProperties.getDurationMaxMessages() > 0 || configurationProperties.getDurationMaxIdleSeconds() > 0) {
                            if (configurationProperties.getDurationMaxMessages() > 0) {
                                LOG.info("CamelSpringBoot will terminate after processing {} messages", configurationProperties.getDurationMaxMessages());
                            }
                            if (configurationProperties.getDurationMaxIdleSeconds() > 0) {
                                LOG.info("CamelSpringBoot will terminate after being idle for more {} seconds", configurationProperties.getDurationMaxIdleSeconds());
                            }
                            // needed by MainDurationEventNotifier to signal when we have processed the max messages
                            final AtomicBoolean completed = new AtomicBoolean();
                            final CountDownLatch latch = new CountDownLatch(1);
                            // register lifecycle so we can trigger to shutdown the JVM when maximum number of messages has been processed
                            EventNotifier notifier = new MainDurationEventNotifier(camelContext, configurationProperties.getDurationMaxMessages(), configurationProperties.getDurationMaxIdleSeconds(), completed, latch, false);
                            // register our event notifier
                            ServiceHelper.startService(notifier);
                            camelContext.getManagementStrategy().addEventNotifier(notifier);
                            terminateApplicationContext(cac, camelContext, latch);
                        }
                    }
                    // start camel manually
                    maybeStart(camelContext);
                }
                for (CamelContextConfiguration camelContextConfiguration : camelContextConfigurations) {
                    LOG.debug("CamelContextConfiguration found. Invoking afterApplicationStart: {}", camelContextConfiguration);
                    camelContextConfiguration.afterApplicationStart(camelContext);
                }
            } catch (Exception e) {
                throw new CamelSpringBootInitializationException(e);
            }
        } else {
            LOG.debug("Camel already started, not adding routes.");
        }
    } else {
        LOG.debug("Ignore ContextRefreshedEvent: {}", event);
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) CountDownLatch(java.util.concurrent.CountDownLatch) MainDurationEventNotifier(org.apache.camel.main.MainDurationEventNotifier) FileNotFoundException(java.io.FileNotFoundException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) MainDurationEventNotifier(org.apache.camel.main.MainDurationEventNotifier) EventNotifier(org.apache.camel.spi.EventNotifier) RoutesBuilder(org.apache.camel.RoutesBuilder)

Example 8 with RoutesBuilder

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

the class AbstractCamelContextFactoryBean method installRoutes.

/**
     * Strategy to install all available routes into the context
     */
protected void installRoutes() throws Exception {
    List<RouteBuilder> builders = new ArrayList<RouteBuilder>();
    // lets add RoutesBuilder's added from references
    if (getBuilderRefs() != null) {
        for (RouteBuilderDefinition builderRef : getBuilderRefs()) {
            RoutesBuilder routes = builderRef.createRoutes(getContext());
            if (routes != null) {
                this.builders.add(routes);
            } else {
                throw new CamelException("Cannot find any routes with this RouteBuilder reference: " + builderRef);
            }
        }
    }
    // install already configured routes
    for (RoutesBuilder routeBuilder : this.builders) {
        getContext().addRoutes(routeBuilder);
    }
    // install builders
    for (RouteBuilder builder : builders) {
        // Inject the annotated resource
        postProcessBeforeInit(builder);
        getContext().addRoutes(builder);
    }
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) CamelException(org.apache.camel.CamelException) ArrayList(java.util.ArrayList) RouteBuilderDefinition(org.apache.camel.model.RouteBuilderDefinition) RoutesBuilder(org.apache.camel.RoutesBuilder)

Example 9 with RoutesBuilder

use of org.apache.camel.RoutesBuilder in project webofneeds by researchstudio-sat.

the class OwnerProtocolCamelConfiguratorImpl method addRouteForEndpoint.

@Override
public synchronized void addRouteForEndpoint(String startingEndpoint, final URI wonNodeURI) throws CamelConfigurationFailedException {
    /**
     * there can be only one route per endpoint. Thus, consuming endpoint of each route shall be unique.
     */
    // todo: using replaceAll might result in security issues. change this.
    String tempStartingComponentName = startingComponent;
    tempStartingComponentName = tempStartingComponentName + endpointMap.get(wonNodeURI).replaceAll(":", "_");
    setStartingEndpoint(wonNodeURI, tempStartingComponentName);
    if (camelContext.getComponent(tempStartingComponentName) == null || camelContext.getRoute(endpointMap.get(wonNodeURI)) == null) {
        // OwnerProtocolDynamicRoutes ownerProtocolRouteBuilder = new OwnerProtocolDynamicRoutes(camelContext, tempStartingComponentName);
        RoutesBuilder ownerProtocolRouteBuilder = createRoutesBuilder(tempStartingComponentName, wonNodeURI);
        try {
            camelContext.addRoutes(ownerProtocolRouteBuilder);
        } catch (Exception e) {
            throw new CamelConfigurationFailedException("adding route to camel context failed", e);
        }
    }
}
Also used : CamelConfigurationFailedException(won.protocol.exception.CamelConfigurationFailedException) RoutesBuilder(org.apache.camel.RoutesBuilder) CamelConfigurationFailedException(won.protocol.exception.CamelConfigurationFailedException)

Example 10 with RoutesBuilder

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

the class CamelModuleWithRouteTypes method routes.

@Provides
Set<RoutesBuilder> routes(Injector injector) {
    Set<RoutesBuilder> answer = Sets.newHashSet();
    for (Class<? extends RoutesBuilder> type : routes) {
        RoutesBuilder route = injector.getInstance(type);
        answer.add(route);
    }
    return answer;
}
Also used : RoutesBuilder(org.apache.camel.RoutesBuilder) Provides(com.google.inject.Provides)

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