Repository: camel Updated Branches: refs/heads/master 19431d32a -> c8dc93bcf
[Spring Boot] Postponed routes injection to the bean post processing phase. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/c8dc93bc Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/c8dc93bc Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/c8dc93bc Branch: refs/heads/master Commit: c8dc93bcfcd56e8f35a9f45097cce2cf6f5eb8be Parents: 19431d3 Author: Henryk Konsek <hekon...@gmail.com> Authored: Thu Nov 6 10:04:20 2014 +0100 Committer: Henryk Konsek <hekon...@gmail.com> Committed: Thu Nov 6 10:04:20 2014 +0100 ---------------------------------------------------------------------- .../spring/boot/CamelAutoConfiguration.java | 15 ++---- .../camel/spring/boot/RoutesCollector.java | 55 +++++++++++++++++++ .../spring/boot/CamelAutoConfigurationTest.java | 57 ++++++++++++++------ .../RouteConfigWithCamelContextInjected.java | 44 +++++++++++++++ 4 files changed, 144 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/c8dc93bc/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 14c1428..c7b6b93 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 @@ -19,7 +19,6 @@ package org.apache.camel.spring.boot; import org.apache.camel.CamelContext; import org.apache.camel.ConsumerTemplate; import org.apache.camel.ProducerTemplate; -import org.apache.camel.RoutesBuilder; import org.apache.camel.TypeConverter; import org.apache.camel.component.properties.PropertiesComponent; import org.apache.camel.component.properties.PropertiesParser; @@ -112,9 +111,6 @@ public class CamelAutoConfiguration { private ApplicationContext applicationContext; @Autowired(required = false) - private RoutesBuilder[] routesBuilders; - - @Autowired(required = false) private CamelContextConfiguration camelContextConfiguration; /** @@ -129,12 +125,6 @@ public class CamelAutoConfiguration { camelContext.disableJMX(); } - if (routesBuilders != null) { - for (RoutesBuilder routesBuilder : routesBuilders) { - camelContext.addRoutes(routesBuilder); - } - } - if (camelContextConfiguration != null) { camelContextConfiguration.beforeStart(camelContext); } @@ -142,6 +132,11 @@ public class CamelAutoConfiguration { return camelContext; } + @Bean + RoutesCollector camelRoutesInjector() { + return new RoutesCollector(); + } + /** * Default producer template for the bootstrapped Camel context. */ http://git-wip-us.apache.org/repos/asf/camel/blob/c8dc93bc/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 new file mode 100644 index 0000000..cedbfe5 --- /dev/null +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/RoutesCollector.java @@ -0,0 +1,55 @@ +/** + * 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.CamelContext; +import org.apache.camel.RoutesBuilder; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +public class RoutesCollector implements BeanPostProcessor, ApplicationContextAware { + + private ApplicationContext applicationContext; + + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + return bean; + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (bean instanceof CamelContext && beanName.equals("camelContext")) { + CamelContext camelContext = (CamelContext) bean; + for (RoutesBuilder routesBuilder : applicationContext.getBeansOfType(RoutesBuilder.class).values()) { + try { + camelContext.addRoutes(routesBuilder); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + } + return bean; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/c8dc93bc/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java index 2b0d8dd..6538539 100644 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java @@ -30,14 +30,16 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import static org.apache.camel.spring.boot.TestConfig.ROUTE_ID; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @RunWith(SpringJUnit4ClassRunner.class) @EnableAutoConfiguration -@SpringApplicationConfiguration(classes = CamelAutoConfigurationTest.class) +@SpringApplicationConfiguration(classes = {TestConfig.class, CamelAutoConfigurationTest.class, RouteConfigWithCamelContextInjected.class}) @IntegrationTest({ "camel.springboot.consumerTemplateCacheSize:100", "camel.springboot.jmxEnabled=false"}) @@ -62,22 +64,7 @@ public class CamelAutoConfigurationTest extends Assert { // Spring context fixtures - String routeId = "testRoute"; - @Bean - CamelContextConfiguration camelContextConfiguration() { - return mock(CamelContextConfiguration.class); - } - - @Bean - RouteBuilder routeBuilder() { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - from("direct:test").routeId(routeId).to("mock:test"); - } - }; - } // Tests @@ -89,7 +76,7 @@ public class CamelAutoConfigurationTest extends Assert { @Test public void shouldDetectRoutes() { // When - Route route = camelContext.getRoute(routeId); + Route route = camelContext.getRoute(ROUTE_ID); // Then assertNotNull(route); @@ -141,4 +128,40 @@ public class CamelAutoConfigurationTest extends Assert { verify(camelContextConfiguration).beforeStart(camelContext); } + @Test + public void shouldStartRoute() { + // Given + String message = "msg"; + + // When + producerTemplate.sendBody("seda:test", message); + String receivedMessage = consumerTemplate.receiveBody("seda:test", String.class); + + // Then + assertEquals(message, receivedMessage); + } + } + +@Configuration +class TestConfig { + + static final String ROUTE_ID = "testRoute"; + + @Bean + RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:test").routeId(ROUTE_ID).to("mock:test"); + } + }; + } + + + @Bean + CamelContextConfiguration camelContextConfiguration() { + return mock(CamelContextConfiguration.class); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/c8dc93bc/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/RouteConfigWithCamelContextInjected.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/RouteConfigWithCamelContextInjected.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/RouteConfigWithCamelContextInjected.java new file mode 100644 index 0000000..8f239b3 --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/RouteConfigWithCamelContextInjected.java @@ -0,0 +1,44 @@ +/** + * 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.CamelContext; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.util.Assert; + +@Configuration +public class RouteConfigWithCamelContextInjected { + + @Autowired + private CamelContext camelContext; + + @Bean + public RoutesBuilder routeCreatedWithInjectedCamelContext() { + Assert.notNull(camelContext); + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("seda:test").to("seda:test"); + } + }; + } + +}