Repository: camel Updated Branches: refs/heads/master 41fca5724 -> 953d1010a
Add Spring Boot REST DSL / JPA example Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/953d1010 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/953d1010 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/953d1010 Branch: refs/heads/master Commit: 953d1010ad40d1cf6d30afe90e6d24784583ce2b Parents: 41fca57 Author: Antonin Stefanutti <anto...@stefanutti.fr> Authored: Fri Sep 2 12:48:30 2016 +0200 Committer: Antonin Stefanutti <anto...@stefanutti.fr> Committed: Fri Sep 2 12:48:30 2016 +0200 ---------------------------------------------------------------------- examples/README.md | 1 + .../spring/boot/metrics/Application.java | 119 +++++++++++++++ .../example/springboot/metrics/Application.java | 119 --------------- .../README.md | 78 ++++++++++ .../camel-example-spring-boot-rest-jpa/pom.xml | 143 +++++++++++++++++++ .../spring/boot/rest/jpa/Application.java | 90 ++++++++++++ .../example/spring/boot/rest/jpa/Book.java | 59 ++++++++ .../spring/boot/rest/jpa/BookRepository.java | 22 +++ .../example/spring/boot/rest/jpa/Database.java | 38 +++++ .../example/spring/boot/rest/jpa/Order.java | 80 +++++++++++ .../spring/boot/rest/jpa/OrderRepository.java | 22 +++ .../spring/boot/rest/jpa/OrderService.java | 38 +++++ .../src/main/resources/application-dev.yml | 32 +++++ .../src/main/resources/application.yml | 52 +++++++ .../src/main/resources/data.sql | 21 +++ .../spring/boot/rest/jpa/ApplicationTest.java | 86 +++++++++++ .../src/test/resources/application.yml | 24 ++++ examples/pom.xml | 1 + 18 files changed, 906 insertions(+), 119 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/README.md ---------------------------------------------------------------------- diff --git a/examples/README.md b/examples/README.md index 3d80923..923e6b7 100644 --- a/examples/README.md +++ b/examples/README.md @@ -85,6 +85,7 @@ All examples have been sort by type/category * [camel-example-spring](camel-example-spring/README.md) * [camel-example-spring-boot](camel-example-spring-boot/README.md) * [camel-example-spring-boot-metrics](camel-example-spring-boot-metrics/README.md) +* [camel-example-spring-boot-rest-sql](camel-example-spring-boot-rest-sql/README.md) * [camel-example-spring-javaconfig](camel-example-spring-javaconfig/README.md) * [camel-example-spring-jms](camel-example-spring-jms/README.md) * [camel-example-spring-security](camel-example-spring-security/README.md) http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-metrics/src/main/java/org/apache/camel/example/spring/boot/metrics/Application.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-metrics/src/main/java/org/apache/camel/example/spring/boot/metrics/Application.java b/examples/camel-example-spring-boot-metrics/src/main/java/org/apache/camel/example/spring/boot/metrics/Application.java new file mode 100644 index 0000000..bbeb2fd --- /dev/null +++ b/examples/camel-example-spring-boot-metrics/src/main/java/org/apache/camel/example/spring/boot/metrics/Application.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.example.springboot.metrics; + +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; + +import com.codahale.metrics.MetricFilter; +import com.codahale.metrics.MetricRegistry; +import com.codahale.metrics.graphite.GraphiteReporter; +import com.codahale.metrics.graphite.GraphiteSender; +import com.codahale.metrics.graphite.GraphiteUDP; +import org.apache.camel.CamelContext; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.metrics.routepolicy.MetricsRoutePolicyFactory; +import org.apache.camel.spring.boot.CamelContextConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +/** + * A simple Spring Boot application, with a couple of timed camel routes + * configured with camel-metrics. Reports metrics to Graphite via + * dropwizard-metrics GraphiteUDP sender. Has standard spring-actuator endpoints + * such as /beans, /autoconfig, /metrics + */ +@SpringBootApplication +public class Application { + + private static final Logger LOG = LoggerFactory.getLogger(Application.class); + + @Autowired + private MetricRegistry metricRegistry; + + /** + * @param args no command line args required + */ + public static void main(String[] args) { + LOG.info(" *** Starting Camel Metrics Example Application ***"); + SpringApplication.run(Application.class, args); + } + + /** + * Create reporter bean and tell Spring to call stop() when shutting down. + * UPD must be enabled in carbon.conf + * + * @return graphite reporter + */ + @Bean(destroyMethod = "stop") + public GraphiteReporter graphiteReporter() { + final GraphiteSender graphite = new GraphiteUDP(new InetSocketAddress("localhost", 2003)); + final GraphiteReporter reporter = GraphiteReporter.forRegistry(metricRegistry).prefixedWith("camel-spring-boot").convertRatesTo(TimeUnit.SECONDS) + .convertDurationsTo(TimeUnit.MILLISECONDS).filter(MetricFilter.ALL).build(graphite); + reporter.start(5, TimeUnit.SECONDS); + return reporter; + } + + /** + * @return timed route that logs output every 6 seconds + */ + @Bean + public RouteBuilder slowRoute() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("timer://foo?period=6000").routeId("slow-route").setBody().constant("Slow hello world!").log("${body}"); + } + }; + } + + /** + * @return timed route that logs output every 2 seconds + */ + @Bean + public RouteBuilder fastRoute() { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("timer://foo?period=2000").routeId("fast-route").setBody().constant("Fast hello world!").log("${body}"); + } + }; + } + + @Bean + CamelContextConfiguration contextConfiguration() { + return new CamelContextConfiguration() { + @Override + public void beforeApplicationStart(CamelContext context) { + LOG.info("Configuring Camel metrics on all routes"); + MetricsRoutePolicyFactory fac = new MetricsRoutePolicyFactory(); + fac.setMetricsRegistry(metricRegistry); + context.addRoutePolicyFactory(fac); + } + + @Override + public void afterApplicationStart(CamelContext camelContext) { + // noop + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-metrics/src/main/java/org/apache/camel/example/springboot/metrics/Application.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-metrics/src/main/java/org/apache/camel/example/springboot/metrics/Application.java b/examples/camel-example-spring-boot-metrics/src/main/java/org/apache/camel/example/springboot/metrics/Application.java deleted file mode 100644 index bbeb2fd..0000000 --- a/examples/camel-example-spring-boot-metrics/src/main/java/org/apache/camel/example/springboot/metrics/Application.java +++ /dev/null @@ -1,119 +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.example.springboot.metrics; - -import java.net.InetSocketAddress; -import java.util.concurrent.TimeUnit; - -import com.codahale.metrics.MetricFilter; -import com.codahale.metrics.MetricRegistry; -import com.codahale.metrics.graphite.GraphiteReporter; -import com.codahale.metrics.graphite.GraphiteSender; -import com.codahale.metrics.graphite.GraphiteUDP; -import org.apache.camel.CamelContext; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.metrics.routepolicy.MetricsRoutePolicyFactory; -import org.apache.camel.spring.boot.CamelContextConfiguration; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; - -/** - * A simple Spring Boot application, with a couple of timed camel routes - * configured with camel-metrics. Reports metrics to Graphite via - * dropwizard-metrics GraphiteUDP sender. Has standard spring-actuator endpoints - * such as /beans, /autoconfig, /metrics - */ -@SpringBootApplication -public class Application { - - private static final Logger LOG = LoggerFactory.getLogger(Application.class); - - @Autowired - private MetricRegistry metricRegistry; - - /** - * @param args no command line args required - */ - public static void main(String[] args) { - LOG.info(" *** Starting Camel Metrics Example Application ***"); - SpringApplication.run(Application.class, args); - } - - /** - * Create reporter bean and tell Spring to call stop() when shutting down. - * UPD must be enabled in carbon.conf - * - * @return graphite reporter - */ - @Bean(destroyMethod = "stop") - public GraphiteReporter graphiteReporter() { - final GraphiteSender graphite = new GraphiteUDP(new InetSocketAddress("localhost", 2003)); - final GraphiteReporter reporter = GraphiteReporter.forRegistry(metricRegistry).prefixedWith("camel-spring-boot").convertRatesTo(TimeUnit.SECONDS) - .convertDurationsTo(TimeUnit.MILLISECONDS).filter(MetricFilter.ALL).build(graphite); - reporter.start(5, TimeUnit.SECONDS); - return reporter; - } - - /** - * @return timed route that logs output every 6 seconds - */ - @Bean - public RouteBuilder slowRoute() { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - from("timer://foo?period=6000").routeId("slow-route").setBody().constant("Slow hello world!").log("${body}"); - } - }; - } - - /** - * @return timed route that logs output every 2 seconds - */ - @Bean - public RouteBuilder fastRoute() { - return new RouteBuilder() { - @Override - public void configure() throws Exception { - from("timer://foo?period=2000").routeId("fast-route").setBody().constant("Fast hello world!").log("${body}"); - } - }; - } - - @Bean - CamelContextConfiguration contextConfiguration() { - return new CamelContextConfiguration() { - @Override - public void beforeApplicationStart(CamelContext context) { - LOG.info("Configuring Camel metrics on all routes"); - MetricsRoutePolicyFactory fac = new MetricsRoutePolicyFactory(); - fac.setMetricsRegistry(metricRegistry); - context.addRoutePolicyFactory(fac); - } - - @Override - public void afterApplicationStart(CamelContext camelContext) { - // noop - } - }; - } - -} http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-rest-jpa/README.md ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-rest-jpa/README.md b/examples/camel-example-spring-boot-rest-jpa/README.md new file mode 100644 index 0000000..0818929 --- /dev/null +++ b/examples/camel-example-spring-boot-rest-jpa/README.md @@ -0,0 +1,78 @@ +# Spring Boot REST DSL / JPA Example + +### Introduction + +This example demonstrates how to use SQL via JDBC along with Camel's REST DSL +to expose a RESTful API that performs CRUD operations on a database. + +It generates orders for books referenced in database at a regular pace. +Orders are processed asynchronously by another Camel route. Books available +in database as well as the status of the generated orders can be retrieved via +the REST API. + +It relies on Swagger to expose the API documentation of the REST service. + +### Build + +You can build this example using: + +```sh +$ mvn package +``` + +### Run + +You can run this example with Maven using: + +```sh +$ mvn spring-boot:run +``` + +Alternatively, you can also run this example using the executable JAR: + +```sh +$ java -jar -Dspring.profiles.active=dev target/camel-example-spring-boot-rest-jpa-${project.version}.jar +``` + +This uses an embedded in-memory HSQLDB database. You can use the default +Spring Boot profile in case you have a MySQL server available for you to test. + +When the Camel application runs, you should see the following messages +being logged to the console, e.g.: + +``` +2016-09-02 09:54:29.702 INFO 27253 --- [mer://new-order] generate-order : Inserted new order 1 +2016-09-02 09:54:31.597 INFO 27253 --- [mer://new-order] generate-order : Inserted new order 2 +2016-09-02 09:54:33.596 INFO 27253 --- [mer://new-order] generate-order : Inserted new order 3 +2016-09-02 09:54:34.637 INFO 27253 --- [rts.camel.Order] process-order : Processed order #id 1 with 7 copies of the «Camel in Action» book +2016-09-02 09:54:34.641 INFO 27253 --- [rts.camel.Order] process-order : Processed order #id 2 with 4 copies of the «Camel in Action» book +2016-09-02 09:54:34.645 INFO 27253 --- [rts.camel.Order] process-order : Processed order #id 3 with 1 copies of the «ActiveMQ in Action» book +2016-09-02 09:54:35.597 INFO 27253 --- [mer://new-order] generate-order : Inserted new order 4 +2016-09-02 09:54:37.601 INFO 27253 --- [mer://new-order] generate-order : Inserted new order 5 +2016-09-02 09:54:39.605 INFO 27253 --- [mer://new-order] generate-order : Inserted new order 6 +2016-09-02 09:54:39.668 INFO 27253 --- [rts.camel.Order] process-order : Processed order #id 4 with 7 copies of the «Camel in Action» book +2016-09-02 09:54:39.671 INFO 27253 --- [rts.camel.Order] process-order : Processed order #id 5 with 1 copies of the «ActiveMQ in Action» book +2016-09-02 09:54:39.674 INFO 27253 --- [rts.camel.Order] process-order : Processed order #id 6 with 4 copies of the «Camel in Action» book +``` + +You can then access the REST API directly from your Web browser, e.g.: + +- <http://localhost:8080/camel-rest-sql/books> +- <http://localhost:8080/camel-rest-sql/books/order/1> + +The Camel application can be stopped pressing <kbd>ctrl</kbd>+<kbd>c</kbd> in the shell. + +### Swagger API + +The example provides API documentation of the service using Swagger using +the _context-path_ `camel-rest-sql/api-doc`. You can access the API documentation +from your Web browser at <http://localhost:8080/camel-rest-sql/api-doc>. + +### Forum, Help, etc + +If you hit an problems please let us know on the Camel Forums +<http://camel.apache.org/discussion-forums.html> + +Please help us make Apache Camel better - we appreciate any feedback you may have. Enjoy! + +The Camel riders! \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-rest-jpa/pom.xml ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-rest-jpa/pom.xml b/examples/camel-example-spring-boot-rest-jpa/pom.xml new file mode 100644 index 0000000..26bc533 --- /dev/null +++ b/examples/camel-example-spring-boot-rest-jpa/pom.xml @@ -0,0 +1,143 @@ +<?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/maven-v4_0_0.xsd"> + + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.camel</groupId> + <artifactId>examples</artifactId> + <version>2.18.0-SNAPSHOT</version> + </parent> + + <artifactId>camel-example-spring-boot-rest-jpa</artifactId> + <name>Camel :: Example :: Spring Boot REST / JPA</name> + <description> + An example demonstrating how to use Camel REST DSL with JPA + to expose a RESTful API that performs CRUD operations on a database + </description> + + <properties> + <spring.boot-version>${spring-boot-version}</spring.boot-version> + <!-- Spring Boot default profile in order to use an embedded database --> + <run.profiles>dev</run.profiles> + </properties> + + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-dependencies</artifactId> + <version>${spring.boot-version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-parent</artifactId> + <version>${project.version}</version> + <type>pom</type> + <scope>import</scope> + </dependency> + <!-- We need to align the Hibernate versions --> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-entitymanager</artifactId> + <version>5.0.9.Final</version> + </dependency> + </dependencies> + </dependencyManagement> + + <dependencies> + + <!-- spring-boot --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-data-jpa</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-spring-boot-starter</artifactId> + </dependency> + + <!-- Camel --> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-servlet</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-jackson</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-swagger-java</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-jpa</artifactId> + </dependency> + + <!-- JDBC --> + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>org.hsqldb</groupId> + <artifactId>hsqldb</artifactId> + <scope>runtime</scope> + </dependency> + + <!-- test --> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + </dependency> + + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + <version>${spring.boot-version}</version> + <executions> + <execution> + <goals> + <goal>repackage</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + +</project> + + + + http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Application.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Application.java b/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Application.java new file mode 100644 index 0000000..6fe265a --- /dev/null +++ b/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Application.java @@ -0,0 +1,90 @@ +/** + * 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.example.spring.boot.rest.jpa; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.servlet.CamelHttpTransportServlet; +import org.apache.camel.model.rest.RestBindingMode; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.boot.web.support.SpringBootServletInitializer; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +@SpringBootApplication +public class Application extends SpringBootServletInitializer { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + @Bean + ServletRegistrationBean servletRegistrationBean() { + ServletRegistrationBean servlet = new ServletRegistrationBean( + new CamelHttpTransportServlet(), "/camel-rest-sql/*"); + servlet.setName("CamelServlet"); + return servlet; + } + + @Component + class RestApi extends RouteBuilder { + + @Override + public void configure() { + restConfiguration() + .contextPath("/camel-rest-sql").apiContextPath("/api-doc") + .apiProperty("api.title", "Camel REST API") + .apiProperty("api.version", "1.0") + .apiProperty("cors", "true") + .apiContextRouteId("doc-api") + .component("servlet") + .bindingMode(RestBindingMode.json); + + rest("/books").description("Books REST service") + .get("/").description("The list of all the books") + .route().routeId("books-api") + .bean(Database.class, "findBooks") + .endRest() + .get("order/{id}").description("Details of an order by id") + .route().routeId("order-api") + .bean(Database.class, "findOrder(${header.id})"); + } + } + + @Component + class Backend extends RouteBuilder { + + @Override + public void configure() { + // A first route generates some orders and queue them in DB + from("timer:new-order?delay=1s&period={{example.generateOrderPeriod:2s}}") + .routeId("generate-order") + .bean("orderService", "generateOrder") + .to("jpa:io.fabric8.quickstarts.camel.Order") + .log("Inserted new order ${body.id}"); + + // A second route polls the DB for new orders and processes them + from("jpa:org.apache.camel.example.spring.boot.rest.jpa.Order" + + "?consumer.namedQuery=new-orders" + + "&consumer.delay={{example.processOrderPeriod:5s}}" + + "&consumeDelete=false") + .routeId("process-order") + .log("Processed order #id ${body.id} with ${body.amount} copies of the «${body.book.description}» book"); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Book.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Book.java b/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Book.java new file mode 100644 index 0000000..9c3cf18 --- /dev/null +++ b/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Book.java @@ -0,0 +1,59 @@ +/** + * 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.example.spring.boot.rest.jpa; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "books") +public class Book { + + @Id + @GeneratedValue + private int id; + + private String item; + + private String description; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getItem() { + return item; + } + + public void setItem(String item) { + this.item = item; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/BookRepository.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/BookRepository.java b/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/BookRepository.java new file mode 100644 index 0000000..afb2098 --- /dev/null +++ b/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/BookRepository.java @@ -0,0 +1,22 @@ +/** + * 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.example.spring.boot.rest.jpa; + +import org.springframework.data.repository.CrudRepository; + +public interface BookRepository extends CrudRepository<Book, Integer> { +} http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Database.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Database.java b/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Database.java new file mode 100644 index 0000000..bd81734 --- /dev/null +++ b/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Database.java @@ -0,0 +1,38 @@ +/** + * 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.example.spring.boot.rest.jpa; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class Database { + + @Autowired + BookRepository books; + + @Autowired + OrderRepository orders; + + public Iterable<Book> findBooks() { + return books.findAll(); + } + + public Order findOrder(Integer id) { + return orders.findOne(id); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Order.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Order.java b/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Order.java new file mode 100644 index 0000000..ebbd781 --- /dev/null +++ b/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/Order.java @@ -0,0 +1,80 @@ +/** + * 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.example.spring.boot.rest.jpa; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.ManyToOne; +import javax.persistence.NamedQuery; +import javax.persistence.Table; + +import org.apache.camel.component.jpa.Consumed; + +@Entity +@Table(name = "orders") +@NamedQuery(name = "new-orders", query = "select order from Order order where order.processed = false") +public class Order { + + @Id + @GeneratedValue + private int id; + + private int amount; + + @ManyToOne + private Book book; + + private boolean processed; + + @Consumed + public void setProcessed() { + processed = true; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getAmount() { + return amount; + } + + public void setAmount(int amount) { + this.amount = amount; + } + + public Book getBook() { + return book; + } + + public void setBook(Book book) { + this.book = book; + } + + public boolean isProcessed() { + return processed; + } + + public void setProcessed(boolean processed) { + this.processed = processed; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/OrderRepository.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/OrderRepository.java b/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/OrderRepository.java new file mode 100644 index 0000000..9ba8718 --- /dev/null +++ b/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/OrderRepository.java @@ -0,0 +1,22 @@ +/** + * 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.example.spring.boot.rest.jpa; + +import org.springframework.data.repository.CrudRepository; + +public interface OrderRepository extends CrudRepository<Order, Integer> { +} http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/OrderService.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/OrderService.java b/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/OrderService.java new file mode 100644 index 0000000..2d26423 --- /dev/null +++ b/examples/camel-example-spring-boot-rest-jpa/src/main/java/org/apache/camel/example/spring/boot/rest/jpa/OrderService.java @@ -0,0 +1,38 @@ +/** + * 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.example.spring.boot.rest.jpa; + +import java.util.Random; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class OrderService { + + @Autowired + private BookRepository books; + + private final Random amount = new Random(); + + public Order generateOrder() { + Order order = new Order(); + order.setAmount(amount.nextInt(10) + 1); + order.setBook(books.findOne(amount.nextInt(2) + 1)); + return order; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-rest-jpa/src/main/resources/application-dev.yml ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-rest-jpa/src/main/resources/application-dev.yml b/examples/camel-example-spring-boot-rest-jpa/src/main/resources/application-dev.yml new file mode 100644 index 0000000..dcc0a56 --- /dev/null +++ b/examples/camel-example-spring-boot-rest-jpa/src/main/resources/application-dev.yml @@ -0,0 +1,32 @@ +# +# 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. +# + +camel: + springboot: + # The Camel context name + name: CamelRestJpa + +spring: + # Spring JDBC configuration + datasource: + # Let Spring Boot auto-configure an embedded HSQL database + url: + +# The application configuration properties +example: + generateOrderPeriod: 2s + processOrderPeriod: 5s http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-rest-jpa/src/main/resources/application.yml ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-rest-jpa/src/main/resources/application.yml b/examples/camel-example-spring-boot-rest-jpa/src/main/resources/application.yml new file mode 100644 index 0000000..5dbe154 --- /dev/null +++ b/examples/camel-example-spring-boot-rest-jpa/src/main/resources/application.yml @@ -0,0 +1,52 @@ +# +# 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. +# + +camel: + springboot: + # The Camel context name + name: CamelRestJpa + +mysql: + service: + # The host of the MySQL database server, can be customized using + # the 'MYSQL_SERVICE_HOST' env variable + host: localhost + # The port of the MySQL database server, can be customized using + # the 'MYSQL_SERVICE_PORT' env variable + port: 3306 + # The database to use, can be customized using the 'MYSQL_SERVICE_DATABASE' + # env variable + database: sampledb + +spring: + # Spring JDBC configuration + datasource: + url: jdbc:mysql://${mysql.service.host}:${mysql.service.port}/${mysql.service.database} + username: ${mysql.service.username} + password: ${mysql.service.password} + + # Spring Data JPA configuration + jpa: + hibernate: + # To be updated in real production usage! + ddl-auto: create-drop + show-sql: false + +# The application configuration properties +example: + generateOrderPeriod: 10s + processOrderPeriod: 30s http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-rest-jpa/src/main/resources/data.sql ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-rest-jpa/src/main/resources/data.sql b/examples/camel-example-spring-boot-rest-jpa/src/main/resources/data.sql new file mode 100644 index 0000000..5698c3d --- /dev/null +++ b/examples/camel-example-spring-boot-rest-jpa/src/main/resources/data.sql @@ -0,0 +1,21 @@ +-- ------------------------------------------------------------------------ +-- 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. +-- ------------------------------------------------------------------------ + +INSERT INTO books (item, description) + VALUES + ('Camel', 'Camel in Action'), + ('ActiveMQ', 'ActiveMQ in Action'); \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-rest-jpa/src/test/java/org/apache/camel/example/spring/boot/rest/jpa/ApplicationTest.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-rest-jpa/src/test/java/org/apache/camel/example/spring/boot/rest/jpa/ApplicationTest.java b/examples/camel-example-spring-boot-rest-jpa/src/test/java/org/apache/camel/example/spring/boot/rest/jpa/ApplicationTest.java new file mode 100644 index 0000000..b2d0d46 --- /dev/null +++ b/examples/camel-example-spring-boot-rest-jpa/src/test/java/org/apache/camel/example/spring/boot/rest/jpa/ApplicationTest.java @@ -0,0 +1,86 @@ +/** + * 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.example.spring.boot.rest.jpa; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.CamelContext; +import org.apache.camel.builder.NotifyBuilder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +public class ApplicationTest { + + @Autowired + private TestRestTemplate restTemplate; + + @Autowired + private CamelContext camelContext; + + @Test + public void newOrderTest() { + // Wait for maximum 5s until the first order gets inserted and processed + NotifyBuilder notify = new NotifyBuilder(camelContext) + .fromRoute("generate-order") + .whenDone(1) + .and() + .fromRoute("process-order") + .whenDone(1) + .create(); + assertThat(notify.matches(5, TimeUnit.SECONDS)).isTrue(); + + // Then call the REST API + ResponseEntity<Order> response = restTemplate.getForEntity("/camel-rest-sql/books/order/1", Order.class); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + Order order = response.getBody(); + assertThat(order.getId()).isEqualTo(1); + assertThat(order.getAmount()).isBetween(1, 10); + assertThat(order.getBook().getItem()).isIn("Camel", "ActiveMQ"); + assertThat(order.getBook().getDescription()).isIn("Camel in Action", "ActiveMQ in Action"); + assertThat(order.isProcessed()).isTrue(); + } + + @Test + public void booksTest() { + ResponseEntity<List<Book>> response = restTemplate.exchange("/camel-rest-sql/books", + HttpMethod.GET, null, new ParameterizedTypeReference<List<Book>>() { + }); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + List<Book> books = response.getBody(); + assertThat(books).hasSize(2); + assertThat(books).element(0) + .hasFieldOrPropertyWithValue("item", "Camel") + .hasFieldOrPropertyWithValue("description", "Camel in Action"); + assertThat(books).element(1) + .hasFieldOrPropertyWithValue("item", "ActiveMQ") + .hasFieldOrPropertyWithValue("description", "ActiveMQ in Action"); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/camel-example-spring-boot-rest-jpa/src/test/resources/application.yml ---------------------------------------------------------------------- diff --git a/examples/camel-example-spring-boot-rest-jpa/src/test/resources/application.yml b/examples/camel-example-spring-boot-rest-jpa/src/test/resources/application.yml new file mode 100644 index 0000000..38b2fc2 --- /dev/null +++ b/examples/camel-example-spring-boot-rest-jpa/src/test/resources/application.yml @@ -0,0 +1,24 @@ +# +# 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. +# + +camel: + springboot: + name: CamelRestJpa + +example: + generateOrderPeriod: 1s + processOrderPeriod: 3s http://git-wip-us.apache.org/repos/asf/camel/blob/953d1010/examples/pom.xml ---------------------------------------------------------------------- diff --git a/examples/pom.xml b/examples/pom.xml index d18df8b..737cfa5 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -80,6 +80,7 @@ <module>camel-example-spring</module> <module>camel-example-spring-boot</module> <module>camel-example-spring-boot-metrics</module> + <module>camel-example-spring-boot-rest-jpa</module> <module>camel-example-spring-boot-starter</module> <module>camel-example-spring-dm</module> <module>camel-example-spring-javaconfig</module>