Repository: camel Updated Branches: refs/heads/master 595976d1d -> a73635400
[CAMEL-7963] Added Spring Boot support. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a7363540 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a7363540 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a7363540 Branch: refs/heads/master Commit: a7363540011693adc8873edf5ab140c62f89cc0d Parents: 595976d Author: Henryk Konsek <[email protected]> Authored: Sun Oct 26 22:41:06 2014 +0100 Committer: Henryk Konsek <[email protected]> Committed: Sun Oct 26 22:41:06 2014 +0100 ---------------------------------------------------------------------- components/camel-spring-boot/pom.xml | 55 +++++++ .../spring/boot/CamelAutoConfiguration.java | 160 +++++++++++++++++++ .../spring/boot/SpringPropertiesParser.java | 35 ++++ .../main/resources/META-INF/spring.factories | 19 +++ .../CamelAutoConfigurationPropertiesTest.java | 79 +++++++++ .../spring/boot/CamelAutoConfigurationTest.java | 119 ++++++++++++++ .../src/test/resources/application.properties | 19 +++ components/pom.xml | 1 + parent/pom.xml | 11 ++ 9 files changed, 498 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a7363540/components/camel-spring-boot/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/pom.xml b/components/camel-spring-boot/pom.xml new file mode 100644 index 0000000..4287f79 --- /dev/null +++ b/components/camel-spring-boot/pom.xml @@ -0,0 +1,55 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>components</artifactId> + <version>2.15-SNAPSHOT</version> + </parent> + + <name>Camel :: Spring Boot</name> + <artifactId>camel-spring-boot</artifactId> + <description>Camel :: Spring Boot autostart configuration</description> + + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-spring</artifactId> + </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + +</project> http://git-wip-us.apache.org/repos/asf/camel/blob/a7363540/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 new file mode 100644 index 0000000..ba42f25 --- /dev/null +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/CamelAutoConfiguration.java @@ -0,0 +1,160 @@ +/** + * 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.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; +import org.apache.camel.spring.SpringCamelContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * <p> + * Opinionated auto-configuration of the Camel context. Auto-detects Camel routes available in the Spring context and + * exposes the key Camel utilities (like producer template, consumer template and type converter). + * </p> + * <p> + * The most important piece of functionality provided by the Camel starter is {@code CamelContext} instance. Camel starter + * will create {@code SpringCamelContext} for your and take care of the proper initialization and shutdown of that context. Created + * Camel context is also registered in the Spring application context (under {@code camelContext} name), so you can access it just + * as the any other Spring bean. + * + * <pre> + * {@literal @}Configuration + * public class MyAppConfig { + * + * {@literal @}Autowired + * CamelContext camelContext; + * + * {@literal @}Bean + * MyService myService() { + * return new DefaultMyService(camelContext); + * } + * + * } + * </pre> + * + * </p> + * <p> + * Camel starter collects all the `RoutesBuilder` instances from the Spring context and automatically injects + * them into the provided {@code CamelContext}. It means that creating new Camel route with the Spring Boot starter is as simple as + * adding the {@code @Component} annotated class into your classpath: + * </p> + * + * <p> + * <pre> + * {@literal @}Component + * public class MyRouter extends RouteBuilder { + * + * {@literal @}Override + * public void configure() throws Exception { + * from("jms:invoices").to("file:/invoices"); + * } + * + * } + * </pre> + * </p> + * + * <p> + * Or creating new route {@code RoutesBuilder} in your {@code @Configuration} class: + * </p> + * <p> + * <pre> + * {@literal @}Configuration + * public class MyRouterConfiguration { + * + * {@literal @}Bean + * RoutesBuilder myRouter() { + * return new RouteBuilder() { + * + * {@literal @}Override + * public void configure() throws Exception { + * from("jms:invoices").to("file:/invoices"); + * } + * + * }; + * } + * + * } + * </pre> + * </p> + */ +@Configuration +public class CamelAutoConfiguration { + + @Autowired + private ApplicationContext applicationContext; + + @Autowired(required = false) + private RoutesBuilder[] routesBuilders; + + /** + * Spring-aware Camel context for the application. Auto-detects and loads all routes available in the Spring + * context. + */ + @Bean + CamelContext camelContext() throws Exception { + CamelContext camelContext = new SpringCamelContext(applicationContext); + if (routesBuilders != null) { + for (RoutesBuilder routesBuilder : routesBuilders) { + camelContext.addRoutes(routesBuilder); + } + } + return camelContext; + } + + /** + * Default producer template for the bootstrapped Camel context. + */ + @Bean + ProducerTemplate producerTemplate() throws Exception { + return camelContext().createProducerTemplate(); + } + + /** + * Default consumer template for the bootstrapped Camel context. + */ + @Bean + ConsumerTemplate consumerTemplate() throws Exception { + return camelContext().createConsumerTemplate(); + } + + @Bean + TypeConverter typeConverter() throws Exception { + return camelContext().getTypeConverter(); + } + + @Bean + PropertiesParser propertiesParser() { + return new SpringPropertiesParser(); + } + + @Bean + PropertiesComponent properties() { + PropertiesComponent properties = new PropertiesComponent(); + properties.setPropertiesParser(propertiesParser()); + return properties; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a7363540/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringPropertiesParser.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringPropertiesParser.java b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringPropertiesParser.java new file mode 100644 index 0000000..3d42116 --- /dev/null +++ b/components/camel-spring-boot/src/main/java/org/apache/camel/spring/boot/SpringPropertiesParser.java @@ -0,0 +1,35 @@ +/** + * 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.Properties; + +import org.apache.camel.component.properties.DefaultPropertiesParser; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.PropertyResolver; + +class SpringPropertiesParser extends DefaultPropertiesParser { + + @Autowired + private PropertyResolver propertyResolver; + + @Override + public String parseProperty(String key, String value, Properties properties) { + return propertyResolver.getProperty(key); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a7363540/components/camel-spring-boot/src/main/resources/META-INF/spring.factories ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/main/resources/META-INF/spring.factories b/components/camel-spring-boot/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..58bee94 --- /dev/null +++ b/components/camel-spring-boot/src/main/resources/META-INF/spring.factories @@ -0,0 +1,19 @@ +# +# 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. +# + +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +org.apache.camel.spring.boot.CamelAutoConfiguration \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a7363540/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationPropertiesTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationPropertiesTest.java b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationPropertiesTest.java new file mode 100644 index 0000000..481d295 --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationPropertiesTest.java @@ -0,0 +1,79 @@ +/** + * 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.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@EnableAutoConfiguration +@SpringApplicationConfiguration(classes = CamelAutoConfigurationPropertiesTest.class) +public class CamelAutoConfigurationPropertiesTest extends Assert { + + // Route fixtures + + @Value("${from}") + String from; + + // Collaborators fixtures + + @Autowired + CamelContext camelContext; + + @Autowired + ProducerTemplate producerTemplate; + + // Spring context fixtures + + @Bean + RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from(from).to("{{to}}"); + } + }; + } + + // Tests + + @Test + public void shouldResolveBothCamelAndSpringPlaceholders() throws InterruptedException { + // Given + MockEndpoint mockEndpoint = camelContext.getEndpoint("mock:test", MockEndpoint.class); + mockEndpoint.expectedMessageCount(1); + + // When + producerTemplate.sendBody(from, "msg"); + + // Then + mockEndpoint.assertIsSatisfied(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a7363540/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 new file mode 100644 index 0000000..eb27aa8 --- /dev/null +++ b/components/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/CamelAutoConfigurationTest.java @@ -0,0 +1,119 @@ +/** + * 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.ConsumerTemplate; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.Route; +import org.apache.camel.TypeConverter; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Assert; +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.SpringApplicationConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@EnableAutoConfiguration +@SpringApplicationConfiguration(classes = CamelAutoConfigurationTest.class) +public class CamelAutoConfigurationTest extends Assert { + + // Collaborators fixtures + + @Autowired + CamelContext camelContext; + + @Autowired + ProducerTemplate producerTemplate; + + @Autowired + ConsumerTemplate consumerTemplate; + + @Autowired + TypeConverter typeConverter; + + // Spring context fixtures + + String routeId = "testRoute"; + + @Bean + RouteBuilder routeBuilder() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:test").routeId(routeId).to("mock:test"); + } + }; + } + + // Tests + + @Test + public void shouldCreateCamelContext() { + assertNotNull(camelContext); + } + + @Test + public void shouldDetectRoutes() { + // When + Route route = camelContext.getRoute(routeId); + + // Then + assertNotNull(route); + } + + @Test + public void shouldLoadProducerTemplate() { + assertNotNull(producerTemplate); + } + + @Test + public void shouldLoadConsumerTemplate() { + assertNotNull(consumerTemplate); + } + + @Test + public void shouldSendAndReceiveMessageWithTemplates() { + // Given + String message = "message"; + String seda = "seda:test"; + + // When + producerTemplate.sendBody(seda, message); + String receivedBody = consumerTemplate.receiveBody(seda, String.class); + + // Then + assertEquals(message, receivedBody); + } + + @Test + public void shouldLoadTypeConverters() { + // Given + Long hundred = 100L; + + // When + Long convertedLong = typeConverter.convertTo(Long.class, hundred.toString()); + + // Then + assertEquals(hundred, convertedLong); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a7363540/components/camel-spring-boot/src/test/resources/application.properties ---------------------------------------------------------------------- diff --git a/components/camel-spring-boot/src/test/resources/application.properties b/components/camel-spring-boot/src/test/resources/application.properties new file mode 100644 index 0000000..ab48a84 --- /dev/null +++ b/components/camel-spring-boot/src/test/resources/application.properties @@ -0,0 +1,19 @@ +# +# 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. +# + +from=direct:test +to=mock:test \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a7363540/components/pom.xml ---------------------------------------------------------------------- diff --git a/components/pom.xml b/components/pom.xml index 9f2a1ef..e8810d1 100644 --- a/components/pom.xml +++ b/components/pom.xml @@ -188,6 +188,7 @@ <module>camel-spark-rest</module> <module>camel-splunk</module> <module>camel-spring-batch</module> + <module>camel-spring-boot</module> <module>camel-spring-javaconfig</module> <module>camel-spring-integration</module> <module>camel-spring-ldap</module> http://git-wip-us.apache.org/repos/asf/camel/blob/a7363540/parent/pom.xml ---------------------------------------------------------------------- diff --git a/parent/pom.xml b/parent/pom.xml index 3e3098d..51d5b3c 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -400,6 +400,7 @@ <spark-rest-version>2.0.0</spark-rest-version> <splunk-version>1.3.0_1</splunk-version> <spring-batch-version>2.2.7.RELEASE</spring-batch-version> + <spring-boot-version>1.1.8.RELEASE</spring-boot-version> <spring-castor-bundle-version>1.2.0</spring-castor-bundle-version> <spring-data-commons-version>1.5.0.RELEASE</spring-data-commons-version> <spring-data-redis-version>1.3.4.RELEASE</spring-data-redis-version> @@ -1282,6 +1283,16 @@ <version>${project.version}</version> </dependency> <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter</artifactId> + <version>${spring-boot-version}</version> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <version>${spring-boot-version}</version> + </dependency> + <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-integration</artifactId> <version>${project.version}</version>
