Repository: camel Updated Branches: refs/heads/master b8128c93c -> 5e0c8dea8
CAMEL-11715: Camel Spring : unable to mix xml a... ...nd java routes Changes the order of `RoutesCollector` so it performs before `CamelContextFactoryBean`. This helps as `CamelContextFactoryBean` will startup `SpringCamelContext` before `RoutesCollector` gets a chance to add routes to the context. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5e0c8dea Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5e0c8dea Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5e0c8dea Branch: refs/heads/master Commit: 5e0c8dea8f8d663028e8e7c3daba08a5f20adb20 Parents: b8128c9 Author: Zoran Regvart <[email protected]> Authored: Tue Sep 5 09:53:28 2017 +0200 Committer: Zoran Regvart <[email protected]> Committed: Tue Sep 5 15:51:55 2017 +0200 ---------------------------------------------------------------------- .../camel/spring/boot/RoutesCollector.java | 14 ++-- .../spring/boot/MixedJavaDslAndXmlTest.java | 78 ++++++++++++++++++++ .../src/test/resources/camel/camelContext.xml | 2 +- .../src/test/resources/test-camel-context.xml | 2 +- 4 files changed, 89 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/5e0c8dea/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 b1edd8c..6e4d588 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 @@ -225,13 +225,17 @@ public class RoutesCollector implements ApplicationListener<ContextRefreshedEven @Override public int getOrder() { // RoutesCollector implements Ordered so that it's the - // second to last in ApplicationListener to receive events, + // first Camel ApplicationListener to receive events, // SpringCamelContext should be the last one, - // CamelContextFactoryBean should be on par with - // RoutesCollector this is important for startup as we want + // CamelContextFactoryBean should be second to last and then + // RoutesCollector. This is important for startup as we want // all resources to be ready and all routes added to the - // context - return LOWEST_PRECEDENCE - 1; + // context before we start CamelContext. + // So the order should be: + // 1. RoutesCollector (LOWEST_PRECEDENCE - 2) + // 2. CamelContextFactoryBean (LOWEST_PRECEDENCE -1) + // 3. SpringCamelContext (LOWEST_PRECEDENCE) + return LOWEST_PRECEDENCE - 2; } // Helpers http://git-wip-us.apache.org/repos/asf/camel/blob/5e0c8dea/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/MixedJavaDslAndXmlTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/MixedJavaDslAndXmlTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/MixedJavaDslAndXmlTest.java new file mode 100644 index 0000000..1dbe394 --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/MixedJavaDslAndXmlTest.java @@ -0,0 +1,78 @@ +/** + * 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 java.util.List; +import java.util.stream.Collectors; + +import org.apache.camel.CamelContext; +import org.apache.camel.Route; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.DefaultRoute; +import org.assertj.core.api.Condition; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class MixedJavaDslAndXmlTest { + + @Configuration + @EnableAutoConfiguration + @ImportResource("classpath:test-camel-context.xml") + public static class JavaDslConfiguration { + + @Bean + public RouteBuilder javaDsl() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("timer:project?period=1s").id("java").setBody().constant("Hello World from Java Route") + .log(">>> ${body}"); + } + }; + } + + } + + @Autowired + private CamelContext camel; + + @Test + public void thereShouldBeTwoRoutesConfigured() { + final List<Route> routes = camel.getRoutes(); + assertThat(routes).as("There should be two routes configured, one from Java DSL and one from XML").hasSize(3); + final List<String> routeIds = routes.stream().map(Route::getId).collect(Collectors.toList()); + assertThat(routeIds).as("Should contain routes from Java DSL, XML and auto-loaded XML").containsOnly("java", + "xml", "xmlAutoLoading"); + assertThat(routes).as("All routes should be started").are(new Condition<Route>() { + @Override + public boolean matches(final Route route) { + return ((DefaultRoute) route).isStarted(); + } + }); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/5e0c8dea/components/camel-spring-boot/src/test/resources/camel/camelContext.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/resources/camel/camelContext.xml b/components/camel-spring-boot/src/test/resources/camel/camelContext.xml index 3b1156a..72ebbe8 100644 --- a/components/camel-spring-boot/src/test/resources/camel/camelContext.xml +++ b/components/camel-spring-boot/src/test/resources/camel/camelContext.xml @@ -18,7 +18,7 @@ --> <routes xmlns="http://camel.apache.org/schema/spring"> - <route> + <route id="xmlAutoLoading"> <from uri="direct:xmlAutoLoading"/> <to uri="mock:xmlAutoLoading"/> </route> http://git-wip-us.apache.org/repos/asf/camel/blob/5e0c8dea/components/camel-spring-boot/src/test/resources/test-camel-context.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/resources/test-camel-context.xml b/components/camel-spring-boot/src/test/resources/test-camel-context.xml index dbe7123..5cb7808 100644 --- a/components/camel-spring-boot/src/test/resources/test-camel-context.xml +++ b/components/camel-spring-boot/src/test/resources/test-camel-context.xml @@ -25,7 +25,7 @@ <!-- Camel Route --> <camelContext xmlns="http://camel.apache.org/schema/spring"> - <route> + <route id="xml"> <from uri="timer://foo?period=1000"/> <setBody><simple>Hello World from camel-contex.xml</simple></setBody> <log message=">>> ${body}"/>
