Search in sources :

Example 1 with RoutesConfigurer

use of org.apache.camel.main.RoutesConfigurer in project camel-spring-boot by apache.

the class CamelSpringBootApplicationListener method onApplicationEvent.

// Overridden
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    CamelContext camelContext = applicationContext.getBean(CamelContext.class);
    // only add and start Camel if its stopped (initial state)
    if (event.getApplicationContext() == this.applicationContext && camelContext.getStatus().isStopped()) {
        LOG.debug("Post-processing CamelContext bean: {}", camelContext.getName());
        try {
            // we can use the default routes configurer
            RoutesConfigurer configurer = new RoutesConfigurer();
            if (configurationProperties.isRoutesCollectorEnabled()) {
                configurer.setRoutesCollector(springBootRoutesCollector);
            }
            configurer.setBeanPostProcessor(camelContext.adapt(ExtendedCamelContext.class).getBeanPostProcessor());
            configurer.setJavaRoutesExcludePattern(configurationProperties.getJavaRoutesExcludePattern());
            configurer.setJavaRoutesIncludePattern(configurationProperties.getJavaRoutesIncludePattern());
            configurer.setRoutesExcludePattern(configurationProperties.getRoutesExcludePattern());
            configurer.setRoutesIncludePattern(configurationProperties.getRoutesIncludePattern());
            configurer.configureRoutes(camelContext);
            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.getMainShutdownStrategy(), true, configurationProperties.isRoutesReloadRestartDuration(), configurationProperties.getDurationMaxAction());
                    // 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.getMainShutdownStrategy(), controller.getMainCompleteTask());
                }
                camelContext.addStartupListener(new StartupListener() {

                    @Override
                    public void onCamelContextStarted(CamelContext context, boolean alreadyStarted) throws Exception {
                        // not running on
                        if (!alreadyStarted) {
                            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 MainShutdownStrategy strategy = new SimpleMainShutdownStrategy();
                        // 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(), strategy, false, configurationProperties.isRoutesReloadRestartDuration(), configurationProperties.getDurationMaxAction());
                        // register our event notifier
                        ServiceHelper.startService(notifier);
                        camelContext.getManagementStrategy().addEventNotifier(notifier);
                        terminateApplicationContext(cac, camelContext, strategy);
                    }
                }
            }
            if (!camelContextConfigurations.isEmpty()) {
                // we want to call these notifications just after CamelContext has been fully started
                // so use an event notifier to trigger when this happens
                camelContext.getManagementStrategy().addEventNotifier(new EventNotifierSupport() {

                    @Override
                    public void notify(CamelEvent eventObject) throws Exception {
                        for (CamelContextConfiguration camelContextConfiguration : camelContextConfigurations) {
                            LOG.debug("CamelContextConfiguration found. Invoking afterApplicationStart: {}", camelContextConfiguration);
                            try {
                                camelContextConfiguration.afterApplicationStart(camelContext);
                            } catch (Exception e) {
                                LOG.warn("Error during calling afterApplicationStart due " + e.getMessage() + ". This exception is ignored", e);
                            }
                        }
                    }

                    @Override
                    public boolean isEnabled(CamelEvent eventObject) {
                        return eventObject.getType() == Type.CamelContextStarted;
                    }
                });
            }
        } catch (Exception e) {
            throw new CamelSpringBootInitializationException(e);
        }
    } else {
        LOG.debug("Camel already started, not adding routes.");
    }
}
Also used : ExtendedCamelContext(org.apache.camel.ExtendedCamelContext) CamelContext(org.apache.camel.CamelContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) StartupListener(org.apache.camel.StartupListener) RoutesConfigurer(org.apache.camel.main.RoutesConfigurer) MainDurationEventNotifier(org.apache.camel.main.MainDurationEventNotifier) CamelEvent(org.apache.camel.spi.CamelEvent) EventNotifierSupport(org.apache.camel.support.EventNotifierSupport) MainDurationEventNotifier(org.apache.camel.main.MainDurationEventNotifier) EventNotifier(org.apache.camel.spi.EventNotifier) SimpleMainShutdownStrategy(org.apache.camel.main.SimpleMainShutdownStrategy) MainShutdownStrategy(org.apache.camel.main.MainShutdownStrategy) SimpleMainShutdownStrategy(org.apache.camel.main.SimpleMainShutdownStrategy)

Example 2 with RoutesConfigurer

use of org.apache.camel.main.RoutesConfigurer in project camel-quarkus by apache.

the class CamelMain method configureRoutes.

protected void configureRoutes(CamelContext camelContext) throws Exception {
    // then configure and add the routes
    RoutesConfigurer configurer = new RoutesConfigurer();
    if (mainConfigurationProperties.isRoutesCollectorEnabled()) {
        configurer.setRoutesCollector(routesCollector);
    }
    configurer.setBeanPostProcessor(camelContext.adapt(ExtendedCamelContext.class).getBeanPostProcessor());
    configurer.setRoutesBuilders(mainConfigurationProperties.getRoutesBuilders());
    configurer.setRoutesExcludePattern(mainConfigurationProperties.getRoutesExcludePattern());
    configurer.setRoutesIncludePattern(mainConfigurationProperties.getRoutesIncludePattern());
    configurer.setJavaRoutesExcludePattern(mainConfigurationProperties.getJavaRoutesExcludePattern());
    configurer.setJavaRoutesIncludePattern(mainConfigurationProperties.getJavaRoutesIncludePattern());
    configurer.configureRoutes(camelContext);
}
Also used : RoutesConfigurer(org.apache.camel.main.RoutesConfigurer)

Example 3 with RoutesConfigurer

use of org.apache.camel.main.RoutesConfigurer in project camel-quarkus by apache.

the class CamelRestOASFilter method exposeOpenAPI.

@BuildStep(onlyIf = ExposeOpenApiEnabled.class)
void exposeOpenAPI(List<CamelRoutesBuilderClassBuildItem> routesBuilderClasses, BuildProducer<AddToOpenAPIDefinitionBuildItem> openAPI, Capabilities capabilities) throws Exception {
    if (capabilities.isPresent(Capability.SMALLRYE_OPENAPI)) {
        RoutesConfigurer configurer = new RoutesConfigurer();
        List<RoutesBuilder> routes = new ArrayList<>();
        configurer.setRoutesBuilders(routes);
        configurer.setRoutesCollector(new DefaultRoutesCollector());
        configurer.setRoutesIncludePattern(CamelSupport.getOptionalConfigValue("camel.main.routes-include-pattern", String.class, null));
        configurer.setRoutesExcludePattern(CamelSupport.getOptionalConfigValue("camel.main.routes-exclude-pattern", String.class, null));
        final CamelContext ctx = new DefaultCamelContext();
        if (!routesBuilderClasses.isEmpty()) {
            final ClassLoader loader = Thread.currentThread().getContextClassLoader();
            if (!(loader instanceof QuarkusClassLoader)) {
                throw new IllegalStateException(QuarkusClassLoader.class.getSimpleName() + " expected as the context class loader");
            }
            for (CamelRoutesBuilderClassBuildItem routesBuilderClass : routesBuilderClasses) {
                final String className = routesBuilderClass.getDotName().toString();
                final Class<?> cl = loader.loadClass(className);
                if (RouteBuilder.class.isAssignableFrom(cl)) {
                    try {
                        final RouteBuilder rb = (RouteBuilder) cl.getDeclaredConstructor().newInstance();
                        routes.add(rb);
                    } catch (InstantiationException | IllegalAccessException e) {
                        throw new RuntimeException("Could not instantiate " + className, e);
                    }
                }
            }
        }
        try {
            configurer.configureRoutes(ctx);
        } catch (Exception e) {
            // ignore
            LOGGER.warn("config routes failed with " + e);
        }
        openAPI.produce(new AddToOpenAPIDefinitionBuildItem(new CamelRestOASFilter(ctx)));
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) CamelRoutesBuilderClassBuildItem(org.apache.camel.quarkus.core.deployment.spi.CamelRoutesBuilderClassBuildItem) AddToOpenAPIDefinitionBuildItem(io.quarkus.smallrye.openapi.deployment.spi.AddToOpenAPIDefinitionBuildItem) RouteBuilder(org.apache.camel.builder.RouteBuilder) ArrayList(java.util.ArrayList) RoutesConfigurer(org.apache.camel.main.RoutesConfigurer) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) QuarkusClassLoader(io.quarkus.bootstrap.classloading.QuarkusClassLoader) DefaultRoutesCollector(org.apache.camel.main.DefaultRoutesCollector) RoutesBuilder(org.apache.camel.RoutesBuilder) BuildStep(io.quarkus.deployment.annotations.BuildStep)

Aggregations

RoutesConfigurer (org.apache.camel.main.RoutesConfigurer)3 CamelContext (org.apache.camel.CamelContext)2 QuarkusClassLoader (io.quarkus.bootstrap.classloading.QuarkusClassLoader)1 BuildStep (io.quarkus.deployment.annotations.BuildStep)1 AddToOpenAPIDefinitionBuildItem (io.quarkus.smallrye.openapi.deployment.spi.AddToOpenAPIDefinitionBuildItem)1 ArrayList (java.util.ArrayList)1 ExtendedCamelContext (org.apache.camel.ExtendedCamelContext)1 RoutesBuilder (org.apache.camel.RoutesBuilder)1 StartupListener (org.apache.camel.StartupListener)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 DefaultRoutesCollector (org.apache.camel.main.DefaultRoutesCollector)1 MainDurationEventNotifier (org.apache.camel.main.MainDurationEventNotifier)1 MainShutdownStrategy (org.apache.camel.main.MainShutdownStrategy)1 SimpleMainShutdownStrategy (org.apache.camel.main.SimpleMainShutdownStrategy)1 CamelRoutesBuilderClassBuildItem (org.apache.camel.quarkus.core.deployment.spi.CamelRoutesBuilderClassBuildItem)1 CamelEvent (org.apache.camel.spi.CamelEvent)1 EventNotifier (org.apache.camel.spi.EventNotifier)1 EventNotifierSupport (org.apache.camel.support.EventNotifierSupport)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1