Repository: camel Updated Branches: refs/heads/master fb59878eb -> 98902f02d
[CAMEL-8460] Fixed double context refresh issue. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/98902f02 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/98902f02 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/98902f02 Branch: refs/heads/master Commit: 98902f02d143b6e81c8d7f0b8048b1788ecbcb50 Parents: fb59878 Author: Henryk Konsek <hekon...@gmail.com> Authored: Tue May 12 18:00:41 2015 +0200 Committer: Henryk Konsek <hekon...@gmail.com> Committed: Tue May 12 18:00:41 2015 +0200 ---------------------------------------------------------------------- .../camel/spring/boot/RoutesCollector.java | 42 ++++++++-------- .../parent/SpringBootWithParentContextTest.java | 51 ++++++++++++++++++++ 2 files changed, 74 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/98902f02/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 c4809f9..2b0a050 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 @@ -51,29 +51,33 @@ public class RoutesCollector implements ApplicationListener<ContextRefreshedEven @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext(); - 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 (applicationContext.getParent() == null) { + 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); + } } - } - loadXmlRoutes(applicationContext, camelContext); + loadXmlRoutes(applicationContext, camelContext); - 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); + } } - } - try { - camelContext.start(); - } catch (Exception e) { - throw new CamelSpringBootInitializationException(e); + try { + camelContext.start(); + } catch (Exception e) { + throw new CamelSpringBootInitializationException(e); + } + } else { + LOG.debug("Not at root context - defer adding routes"); } } http://git-wip-us.apache.org/repos/asf/camel/blob/98902f02/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/parent/SpringBootWithParentContextTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/parent/SpringBootWithParentContextTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/parent/SpringBootWithParentContextTest.java new file mode 100644 index 0000000..6a1c16a --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/parent/SpringBootWithParentContextTest.java @@ -0,0 +1,51 @@ +/** + * 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.parent; + +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.support.GenericApplicationContext; + +public class SpringBootWithParentContextTest { + + @Test + public void shouldCollectRoutesOnlyInRootContext() { + GenericApplicationContext parent = new GenericApplicationContext(); + parent.refresh(); + new SpringApplicationBuilder(Configuration.class).web(false).parent(parent).run(); + } + +} + +@SpringBootApplication +class Configuration { + + @Bean + RoutesBuilder routes() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("seda:test").to("mock:test"); + } + }; + } + +} \ No newline at end of file