CAMEL-8460: Better logic for trigger when to start Camel on the refresh event. 
This fixes #668


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

Branch: refs/heads/camel-2.15.x
Commit: 5d266ce2c026a5301a2e4e2500358934b74b2c47
Parents: b652b86
Author: Claus Ibsen <davscl...@apache.org>
Authored: Wed Nov 11 08:37:24 2015 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Wed Nov 11 08:39:17 2015 +0100

----------------------------------------------------------------------
 .../camel/spring/boot/RoutesCollector.java      | 44 +++++++++++---------
 1 file changed, 25 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/5d266ce2/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 793b1e4..1a4ffda 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
@@ -45,7 +45,7 @@ public class RoutesCollector implements 
ApplicationListener<ContextRefreshedEven
     // Constructors
 
     public RoutesCollector(ApplicationContext applicationContext, 
List<CamelContextConfiguration> camelContextConfigurations) {
-       this.applicationContext = applicationContext;
+        this.applicationContext = applicationContext;
         this.camelContextConfigurations = new 
ArrayList<CamelContextConfiguration>(camelContextConfigurations);
     }
 
@@ -57,30 +57,36 @@ public class RoutesCollector implements 
ApplicationListener<ContextRefreshedEven
         // only listen to context refreshs of "my" applicationContext
         if (this.applicationContext.equals(applicationContext)) {
             CamelContext camelContext = 
contextRefreshedEvent.getApplicationContext().getBean(CamelContext.class);
-            LOG.debug("Post-processing CamelContext bean: {}", 
camelContext.getName());
-            for (RoutesBuilder routesBuilder : 
applicationContext.getBeansOfType(RoutesBuilder.class).values()) {
-                try {
-                    LOG.debug("Injecting following route into the 
CamelContext: {}", routesBuilder);
-                    camelContext.addRoutes(routesBuilder);
-                } catch (Exception e) {
-                    throw new CamelSpringBootInitializationException(e);
+
+            // 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).values()) {
+                    try {
+                        LOG.debug("Injecting following route into the 
CamelContext: {}", routesBuilder);
+                        camelContext.addRoutes(routesBuilder);
+                    } catch (Exception e) {
+                        throw new CamelSpringBootInitializationException(e);
+                    }
                 }
-            }
 
-            try {
-                loadXmlRoutes(applicationContext, camelContext);
+                try {
+                    loadXmlRoutes(applicationContext, camelContext);
 
-                for (CamelContextConfiguration camelContextConfiguration : 
camelContextConfigurations) {
-                    LOG.debug("CamelContextConfiguration found. Invoking: {}", 
camelContextConfiguration);
-                    
camelContextConfiguration.beforeApplicationStart(camelContext);
-                }
+                    for (CamelContextConfiguration camelContextConfiguration : 
camelContextConfigurations) {
+                        LOG.debug("CamelContextConfiguration found. Invoking: 
{}", camelContextConfiguration);
+                        
camelContextConfiguration.beforeApplicationStart(camelContext);
+                    }
 
-                camelContext.start();
-            } catch (Exception e) {
-                throw new CamelSpringBootInitializationException(e);
+                    camelContext.start();
+                } catch (Exception e) {
+                    throw new CamelSpringBootInitializationException(e);
+                }
+            } else {
+                LOG.debug("Camel already started, not adding routes.");
             }
         } else {
-            LOG.debug("Camel already started, not adding routes.");
+            LOG.debug("Ignore ContextRefreshedEvent: {}", 
contextRefreshedEvent);
         }
     }
 

Reply via email to