CAMEL-9161 Change for dealing with parent contexts
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/9c87aed7 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/9c87aed7 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/9c87aed7 Branch: refs/heads/camel-2.16.x Commit: 9c87aed7625b2280298e93f0c0f5a8e4d1937018 Parents: 4404144 Author: Nick Stuart <n...@portlandwebworks.com> Authored: Tue Nov 10 09:19:07 2015 -0500 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Nov 11 08:18:56 2015 +0100 ---------------------------------------------------------------------- .../camel/spring/boot/RoutesCollector.java | 5 +- .../parent/SpringBootRefreshContextTest.java | 57 ++++++++++++++++++++ .../parent/SpringBootWithParentContextTest.java | 51 ------------------ 3 files changed, 60 insertions(+), 53 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/9c87aed7/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 e5dc610..b2fcd77 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 @@ -22,6 +22,7 @@ import java.util.List; import org.apache.camel.CamelContext; import org.apache.camel.RoutesBuilder; +import org.apache.camel.ServiceStatus; import org.apache.camel.model.RoutesDefinition; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -55,8 +56,8 @@ public class RoutesCollector implements ApplicationListener<ContextRefreshedEven @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext(); - if (applicationContext.getParent() == null) { - CamelContext camelContext = contextRefreshedEvent.getApplicationContext().getBean(CamelContext.class); + CamelContext camelContext = contextRefreshedEvent.getApplicationContext().getBean(CamelContext.class); + if (camelContext.getStatus() == ServiceStatus.Stopped) { LOG.debug("Post-processing CamelContext bean: {}", camelContext.getName()); for (RoutesBuilder routesBuilder : applicationContext.getBeansOfType(RoutesBuilder.class).values()) { try { http://git-wip-us.apache.org/repos/asf/camel/blob/9c87aed7/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/parent/SpringBootRefreshContextTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/parent/SpringBootRefreshContextTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/parent/SpringBootRefreshContextTest.java new file mode 100644 index 0000000..dd2dc3b --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/parent/SpringBootRefreshContextTest.java @@ -0,0 +1,57 @@ +/** + * 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.apache.camel.spring.boot.RoutesCollector; +import org.junit.Test; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.context.support.GenericApplicationContext; + +public class SpringBootRefreshContextTest { + + @Test + public void shouldOnlyCollectRoutesOnce() { + GenericApplicationContext parent = new GenericApplicationContext(); + parent.refresh(); + ConfigurableApplicationContext context = new SpringApplicationBuilder(Configuration.class).web(false).parent(parent).run(); + ContextRefreshedEvent refreshEvent = new ContextRefreshedEvent(context); + RoutesCollector collector = context.getBean(RoutesCollector.class); + collector.onApplicationEvent(refreshEvent); //no changes should happen here + } + +} + +@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 http://git-wip-us.apache.org/repos/asf/camel/blob/9c87aed7/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 deleted file mode 100644 index 6a1c16a..0000000 --- a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/parent/SpringBootWithParentContextTest.java +++ /dev/null @@ -1,51 +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.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