You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by he...@apache.org on 2015/03/04 22:01:40 UTC

camel git commit: [CAMEL-8436][Spring Boot] Delayed CamelContext startup to Spring context refresh.

Repository: camel
Updated Branches:
  refs/heads/master f34705754 -> 1c1953ac4


[CAMEL-8436][Spring Boot] Delayed CamelContext startup to Spring context refresh.


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

Branch: refs/heads/master
Commit: 1c1953ac4dc2319403101b5f04a31e53cdf615f1
Parents: f347057
Author: Henryk Konsek <he...@gmail.com>
Authored: Wed Mar 4 22:01:35 2015 +0100
Committer: Henryk Konsek <he...@gmail.com>
Committed: Wed Mar 4 22:01:35 2015 +0100

----------------------------------------------------------------------
 .../spring/boot/CamelAutoConfiguration.java     |  1 +
 .../CamelSpringBootInitializationException.java | 25 ++++++++
 .../camel/spring/boot/RoutesCollector.java      | 54 +++++++----------
 .../spring/boot/DuplicatedRouteIdTest.java      | 61 --------------------
 .../boot/componentroute/ComponentRoute.java     |  6 ++
 .../DuplicatedRouteIdTest.java                  | 61 ++++++++++++++++++++
 6 files changed, 115 insertions(+), 93 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1c1953ac/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
index 3931c84..aba9623 100644
--- a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
+++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java
@@ -46,6 +46,7 @@ public class CamelAutoConfiguration {
     CamelContext camelContext(ApplicationContext applicationContext,
                               CamelConfigurationProperties configurationProperties) {
         CamelContext camelContext = new SpringCamelContext(applicationContext);
+        SpringCamelContext.setNoStart(true);
 
         if (!configurationProperties.isJmxEnabled()) {
             camelContext.disableJMX();

http://git-wip-us.apache.org/repos/asf/camel/blob/1c1953ac/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootInitializationException.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootInitializationException.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootInitializationException.java
new file mode 100644
index 0000000..67e0399
--- /dev/null
+++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelSpringBootInitializationException.java
@@ -0,0 +1,25 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.spring.boot;
+
+public class CamelSpringBootInitializationException extends RuntimeException {
+
+    public CamelSpringBootInitializationException(Throwable cause) {
+        super(cause);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1c1953ac/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 e5cc977..b0c501a 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
@@ -19,16 +19,14 @@ package org.apache.camel.spring.boot;
 import java.util.List;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.Ordered;
 import org.apache.camel.RoutesBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.config.BeanPostProcessor;
 import org.springframework.context.ApplicationContext;
-import org.springframework.core.PriorityOrdered;
+import org.springframework.context.ApplicationListener;
+import org.springframework.context.event.ContextRefreshedEvent;
 
-public class RoutesCollector implements BeanPostProcessor, PriorityOrdered {
+public class RoutesCollector implements ApplicationListener<ContextRefreshedEvent> {
 
     private static final Logger LOG = LoggerFactory.getLogger(RoutesCollector.class);
 
@@ -48,36 +46,28 @@ public class RoutesCollector implements BeanPostProcessor, PriorityOrdered {
     // Overridden
 
     @Override
-    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
-        if (bean instanceof CamelContext && beanName.equals("camelContext")) {
-            CamelContext camelContext = (CamelContext) bean;
-            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 RuntimeException(e);
-                }
+    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
+        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 RuntimeException(e);
             }
-            if (camelContextConfigurations != null) {
-                for (CamelContextConfiguration camelContextConfiguration : camelContextConfigurations) {
-                    LOG.debug("CamelContextConfiguration found. Invoking: {}", camelContextConfiguration);
-                    camelContextConfiguration.beforeApplicationStart(camelContext);
-                }
+        }
+        if (camelContextConfigurations != null) {
+            for (CamelContextConfiguration camelContextConfiguration : camelContextConfigurations) {
+                LOG.debug("CamelContextConfiguration found. Invoking: {}", camelContextConfiguration);
+                camelContextConfiguration.beforeApplicationStart(camelContext);
             }
         }
-        return bean;
-    }
-
-    @Override
-    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
-        return bean;
-    }
-
-    @Override
-    public int getOrder() {
-        return Ordered.LOWEST;
+        try {
+            camelContext.start();
+        } catch (Exception e) {
+            throw new CamelSpringBootInitializationException(e);
+        }
     }
 
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/1c1953ac/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/DuplicatedRouteIdTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/DuplicatedRouteIdTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/DuplicatedRouteIdTest.java
deleted file mode 100644
index eb5feb1..0000000
--- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/DuplicatedRouteIdTest.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.camel.spring.boot;
-
-import org.apache.camel.RoutesBuilder;
-import org.apache.camel.builder.RouteBuilder;
-import org.junit.Assert;
-import org.junit.Test;
-import org.springframework.beans.factory.BeanCreationException;
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.context.annotation.Bean;
-
-public class DuplicatedRouteIdTest extends Assert {
-
-    @Test(expected = BeanCreationException.class)
-    public void shouldDetectDuplicatedRouteId() {
-        new SpringApplication(DuplicatedRouteIdTestConfiguration.class).run();
-    }
-
-}
-
-@SpringBootApplication
-class DuplicatedRouteIdTestConfiguration {
-
-    @Bean
-    RoutesBuilder firstRoute() {
-        return new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("seda:first").routeId("foo").to("mock:first");
-            }
-        };
-    }
-
-    @Bean
-    RoutesBuilder secondRoute() {
-        return new RouteBuilder() {
-            @Override
-            public void configure() throws Exception {
-                from("seda:second").routeId("foo").to("mock:second");
-            }
-        };
-    }
-
-
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/1c1953ac/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/componentroute/ComponentRoute.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/componentroute/ComponentRoute.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/componentroute/ComponentRoute.java
index 9dad981..984179c 100644
--- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/componentroute/ComponentRoute.java
+++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/componentroute/ComponentRoute.java
@@ -16,12 +16,18 @@
  */
 package org.apache.camel.spring.boot.componentroute;
 
+import org.apache.camel.ProducerTemplate;
 import org.apache.camel.builder.RouteBuilder;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
 @Component
 public class ComponentRoute extends RouteBuilder {
 
+    // We inject this field to make sure that template creation doesn't affects routes collecting.
+    @Autowired
+    ProducerTemplate producerTemplate;
+
     @Override
     public void configure() throws Exception {
         from("direct:componentRoute").to("mock:componentRoute");

http://git-wip-us.apache.org/repos/asf/camel/blob/1c1953ac/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/duplicatedrouteid/DuplicatedRouteIdTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/duplicatedrouteid/DuplicatedRouteIdTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/duplicatedrouteid/DuplicatedRouteIdTest.java
new file mode 100644
index 0000000..1d6185f
--- /dev/null
+++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/duplicatedrouteid/DuplicatedRouteIdTest.java
@@ -0,0 +1,61 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.spring.boot.duplicatedrouteid;
+
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spring.boot.CamelSpringBootInitializationException;
+import org.junit.Assert;
+import org.junit.Test;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+
+public class DuplicatedRouteIdTest extends Assert {
+
+    @Test(expected = CamelSpringBootInitializationException.class)
+    public void shouldDetectDuplicatedRouteId() {
+        new SpringApplication(DuplicatedRouteIdTestConfiguration.class).run();
+    }
+
+}
+
+@SpringBootApplication
+class DuplicatedRouteIdTestConfiguration {
+
+    @Bean
+    RoutesBuilder firstRoute() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("seda:first").routeId("foo").to("mock:first");
+            }
+        };
+    }
+
+    @Bean
+    RoutesBuilder secondRoute() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("seda:second").routeId("foo").to("mock:second");
+            }
+        };
+    }
+
+
+}
\ No newline at end of file