You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by zr...@apache.org on 2017/06/29 12:31:54 UTC

camel git commit: CAMEL-11476: spring-boot - routes not loaded wh...

Repository: camel
Updated Branches:
  refs/heads/master e621beba5 -> 2f472eda3


CAMEL-11476: spring-boot - routes not loaded wh...

...en setting a management.port

Fixes `RouteBuilder` (Java DSL) based routes not loading when setting
`management.port` Spring Boot property by removing the 'same application
context' check.

Also removes `maybeStart` method to start the Camel context from
`RoutesCollector`, startup is now handled in `SpringCamelContext`.

When customizing the `management.port` configuration option Spring Boot
creates a new `ApplicationContext` instance which when started emits a
`ContextRefreshedEvent` that is different from the `ApplicationContext`
the `RoutesCollector` is initialized with.

Due to change in CAMEL-11261, `SpringCamelContext` is started via the
same `ContextRefreshedEvent`, at the point the second
`ContextRefreshedEvent` emited from the `ApplicationContext`, that
`RoutesCollector` is initialized with, `SpringCamelContext` is already
started and no Java DSL routes are added to Camel context.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2f472eda
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2f472eda
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2f472eda

Branch: refs/heads/master
Commit: 2f472eda3863ae85bad8fc028805b9340bdb5000
Parents: e621beb
Author: Zoran Regvart <zr...@apache.org>
Authored: Thu Jun 29 10:42:07 2017 +0200
Committer: Zoran Regvart <zr...@apache.org>
Committed: Thu Jun 29 14:31:41 2017 +0200

----------------------------------------------------------------------
 .../camel/spring/boot/RoutesCollector.java      | 174 ++++++++-----------
 1 file changed, 76 insertions(+), 98 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/2f472eda/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java
index d14ef06..fdd4601 100644
--- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java
+++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java
@@ -75,122 +75,111 @@ public class RoutesCollector implements ApplicationListener<ContextRefreshedEven
 
     @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);
-                        }
+        CamelContext camelContext = applicationContext.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());
-                    }
+            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);
+                }
 
-                    boolean scanRests = !configurationProperties.getXmlRests().equals("false");
-                    if (scanRests) {
-                        loadXmlRests(applicationContext, camelContext, configurationProperties.getXmlRests());
+                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);
                     }
 
-                    for (CamelContextConfiguration camelContextConfiguration : camelContextConfigurations) {
-                        LOG.debug("CamelContextConfiguration found. Invoking beforeApplicationStart: {}", camelContextConfiguration);
-                        camelContextConfiguration.beforeApplicationStart(camelContext);
+                    if (configurationProperties.getDurationMaxSeconds() > 0) {
+                        LOG.info("CamelSpringBoot will terminate after {} seconds", configurationProperties.getDurationMaxSeconds());
+                        terminateMainControllerAfter(camelContext, configurationProperties.getDurationMaxSeconds(),
+                            controller.getCompleted(), controller.getLatch());
                     }
 
-                    if (configurationProperties.isMainRunController()) {
-                        CamelMainRunController controller = new CamelMainRunController(applicationContext, camelContext);
+                    // 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(),
-                                controller.getCompleted(), controller.getLatch(), true);
+                                completed, latch, false);
                             // 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());
+                            terminateApplicationContext(cac, camelContext, latch);
                         }
-
-                        // 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);
+                for (CamelContextConfiguration camelContextConfiguration : camelContextConfigurations) {
+                    LOG.debug("CamelContextConfiguration found. Invoking afterApplicationStart: {}", camelContextConfiguration);
+                    camelContextConfiguration.afterApplicationStart(camelContext);
                 }
-            } else {
-                LOG.debug("Camel already started, not adding routes.");
+            } catch (Exception e) {
+                throw new CamelSpringBootInitializationException(e);
             }
         } else {
-            LOG.debug("Ignore ContextRefreshedEvent: {}", event);
+            LOG.debug("Camel already started, not adding routes.");
         }
     }
 
@@ -206,17 +195,6 @@ public class RoutesCollector implements ApplicationListener<ContextRefreshedEven
         return LOWEST_PRECEDENCE - 1;
     }
 
-    private void maybeStart(CamelContext camelContext) throws Exception {
-        // for example from unit testing we want to start Camel later and not when Spring framework
-        // publish a ContextRefreshedEvent
-        boolean skip = "true".equalsIgnoreCase(System.getProperty("skipStartingCamelContext"));
-        if (skip) {
-            LOG.info("Skipping starting CamelContext as system property skipStartingCamelContext is set to be true.");
-        } else {
-            camelContext.start();
-        }
-    }
-
     // Helpers
 
     private void loadXmlRoutes(ApplicationContext applicationContext, CamelContext camelContext, String directory) throws Exception {