public void onApplicationEvent()

in core/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootApplicationListener.java [85:234]


    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.getMain().isRoutesCollectorEnabled()) {
                    configurer.setRoutesCollector(springBootRoutesCollector);
                }

                configurer.setBeanPostProcessor(PluginHelper.getBeanPostProcessor(camelContext));
                configurer.setJavaRoutesExcludePattern(configurationProperties.getMain().getJavaRoutesExcludePattern());
                configurer.setJavaRoutesIncludePattern(configurationProperties.getMain().getJavaRoutesIncludePattern());
                configurer.setRoutesExcludePattern(configurationProperties.getMain().getRoutesExcludePattern());
                configurer.setRoutesIncludePattern(configurationProperties.getMain().getRoutesIncludePattern());
                configurer.configureRoutes(camelContext);

                for (CamelContextConfiguration camelContextConfiguration : camelContextConfigurations) {
                    LOG.debug("CamelContextConfiguration found. Invoking beforeApplicationStart: {}",
                            camelContextConfiguration);
                    camelContextConfiguration.beforeApplicationStart(camelContext);
                }

                if (configurationProperties.getSpringboot().isMainRunController()) {
                    CamelMainRunController controller = new CamelMainRunController(applicationContext, camelContext);

                    if (configurationProperties.getMain().getDurationMaxMessages() > 0
                            || configurationProperties.getMain().getDurationMaxIdleSeconds() > 0) {
                        if (configurationProperties.getMain().getDurationMaxMessages() > 0) {
                            LOG.info("CamelSpringBoot will terminate after processing {} messages",
                                    configurationProperties.getMain().getDurationMaxMessages());
                        }
                        if (configurationProperties.getMain().getDurationMaxIdleSeconds() > 0) {
                            LOG.info("CamelSpringBoot will terminate after being idle for more {} seconds",
                                    configurationProperties.getMain().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.getMain().getDurationMaxMessages(),
                                configurationProperties.getMain().getDurationMaxIdleSeconds(),
                                controller.getMainShutdownStrategy(), true,
                                configurationProperties.getMain().isRoutesReloadRestartDuration(),
                                configurationProperties.getMain().getDurationMaxAction());
                        // register our event notifier
                        ServiceHelper.startService(notifier);
                        camelContext.getManagementStrategy().addEventNotifier(notifier);
                    }

                    if (configurationProperties.getMain().getDurationMaxSeconds() > 0) {
                        LOG.info("CamelSpringBoot will terminate after {} seconds",
                                configurationProperties.getMain().getDurationMaxSeconds());
                        terminateMainControllerAfter(camelContext,
                                configurationProperties.getMain().getDurationMaxSeconds(),
                                controller.getMainShutdownStrategy(), controller.getMainCompleteTask());
                    }

                    camelContext.addStartupListener(new StartupListener() {
                        @Override
                        public void onCamelContextStarted(CamelContext context, boolean alreadyStarted)
                                throws Exception {
                            // run the CamelMainRunController after the context has been started
                            // this way we ensure that NO_START flag is honoured as it's set as
                            // a thread local variable of the thread CamelMainRunController is
                            // 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.getMain().getDurationMaxSeconds() > 0) {
                            LOG.info("CamelSpringBoot will terminate after {} seconds",
                                    configurationProperties.getMain().getDurationMaxSeconds());
                            terminateApplicationContext(cac, camelContext,
                                    configurationProperties.getMain().getDurationMaxSeconds());
                        }

                        if (configurationProperties.getMain().getDurationMaxMessages() > 0
                                || configurationProperties.getMain().getDurationMaxIdleSeconds() > 0) {

                            if (configurationProperties.getMain().getDurationMaxMessages() > 0) {
                                LOG.info("CamelSpringBoot will terminate after processing {} messages",
                                        configurationProperties.getMain().getDurationMaxMessages());
                            }
                            if (configurationProperties.getMain().getDurationMaxIdleSeconds() > 0) {
                                LOG.info("CamelSpringBoot will terminate after being idle for more {} seconds",
                                        configurationProperties.getMain().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.getMain().getDurationMaxMessages(),
                                    configurationProperties.getMain().getDurationMaxIdleSeconds(), strategy, false,
                                    configurationProperties.getMain().isRoutesReloadRestartDuration(),
                                    configurationProperties.getMain().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 {}. This exception is ignored",
                                            e.getMessage(), 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.");
        }
    }