You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/03/07 09:52:20 UTC

camel git commit: Add little rest producer example

Repository: camel
Updated Branches:
  refs/heads/master 080e40dfa -> 4c8eab4e7


Add little rest producer example


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/4c8eab4e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4c8eab4e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4c8eab4e

Branch: refs/heads/master
Commit: 4c8eab4e7358865a8ba09bc395232fcadb9260f3
Parents: 080e40d
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Mar 7 10:52:00 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Mar 7 10:52:14 2017 +0100

----------------------------------------------------------------------
 examples/README.adoc                            |   4 +-
 examples/camel-example-rest-producer/pom.xml    | 127 +++++++++++++++++++
 .../camel-example-rest-producer/readme.adoc     |  34 +++++
 .../main/java/sample/camel/PetController.java   |  43 +++++++
 .../main/java/sample/camel/RestApplication.java |  37 ++++++
 .../src/main/java/sample/camel/RestRoute.java   |  41 ++++++
 .../src/main/resources/application.properties   |  25 ++++
 .../java/sample/camel/RestApplicationTest.java  |  47 +++++++
 8 files changed, 357 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/4c8eab4e/examples/README.adoc
----------------------------------------------------------------------
diff --git a/examples/README.adoc b/examples/README.adoc
index de1e672..5547818 100644
--- a/examples/README.adoc
+++ b/examples/README.adoc
@@ -11,7 +11,7 @@ View the individual example READMEs for details.
 ### Examples
 
 // examples: START
-Number of Examples: 86 (7 deprecated)
+Number of Examples: 87 (7 deprecated)
 
 [width="100%",cols="4,2,4",options="header"]
 |=======================================================================
@@ -160,6 +160,8 @@ Number of Examples: 86 (7 deprecated)
 
 | link:camel-example-cdi-rest-servlet/README.md[CDI Rest] (camel-example-cdi-rest-servlet) | Rest | REST DSL / Servlet with CDI example
 
+| link:camel-example-rest-producer/readme.adoc[Rest Producer] (camel-example-rest-producer) | Rest | An example showing how to use Camel Rest to call a REST service
+
 | link:camel-example-restlet-jdbc/README.md[Restlet JDBC] (camel-example-restlet-jdbc) | Rest | An example showing how to create REST API with Camel Restlet and JDBC components
 
 | link:camel-example-spark-rest/README.md[Spark REST] (camel-example-spark-rest) | Rest | An example using Spark REST

http://git-wip-us.apache.org/repos/asf/camel/blob/4c8eab4e/examples/camel-example-rest-producer/pom.xml
----------------------------------------------------------------------
diff --git a/examples/camel-example-rest-producer/pom.xml b/examples/camel-example-rest-producer/pom.xml
new file mode 100644
index 0000000..6954127
--- /dev/null
+++ b/examples/camel-example-rest-producer/pom.xml
@@ -0,0 +1,127 @@
+<?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.19.0-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>camel-example-rest-producer</artifactId>
+  <name>Camel :: Example :: Rest Producer</name>
+  <description>An example showing how to use Camel Rest to call a REST service</description>
+
+  <properties>
+    <category>Rest</category>
+
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
+    <spring.boot-version>${spring-boot-version}</spring.boot-version>
+  </properties>
+
+  <dependencyManagement>
+    <dependencies>
+      <!-- Spring Boot BOM -->
+      <dependency>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-dependencies</artifactId>
+        <version>${spring.boot-version}</version>
+        <type>pom</type>
+        <scope>import</scope>
+      </dependency>
+      <!-- Camel BOM -->
+      <dependency>
+        <groupId>org.apache.camel</groupId>
+        <artifactId>camel-spring-boot-dependencies</artifactId>
+        <version>${project.version}</version>
+        <type>pom</type>
+        <scope>import</scope>
+      </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-undertow</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-actuator</artifactId>
+    </dependency>
+
+    <!-- Camel -->
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-spring-boot-starter</artifactId>
+    </dependency>
+
+    <!-- support swagger api-doc -->
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-swagger-java-starter</artifactId>
+    </dependency>
+
+    <!-- use http4 as rest producer -->
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-http4-starter</artifactId>
+    </dependency>
+
+    <!-- test -->
+    <dependency>
+      <groupId>org.springframework.boot</groupId>
+      <artifactId>spring-boot-starter-test</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-test-spring</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/4c8eab4e/examples/camel-example-rest-producer/readme.adoc
----------------------------------------------------------------------
diff --git a/examples/camel-example-rest-producer/readme.adoc b/examples/camel-example-rest-producer/readme.adoc
new file mode 100644
index 0000000..3798bc8
--- /dev/null
+++ b/examples/camel-example-rest-producer/readme.adoc
@@ -0,0 +1,34 @@
+# Camel Example Rest Producer
+
+This example shows how to call a REST service using Camel Rest.
+
+The example uses a timer to trigger a Camel route to call the REST service.
+The REST service is embedded within Spring Boot itself so it can run standalone.
+
+## Camel routes
+
+The Camel route is located in the `RestRoute` class. In this class the route
+starts from a timer, that triggers every 2nd second and calls the REST service using the rest endpoint
+which returns a message, that is logged.
+
+## Configuring Rest
+
+The rest producer is configured using Camels Rest DSL which is done using the `restConfiguration` in the route.
+The actual HTTP client that is used to call the REST service is `camel-http4` which is added as dependency
+in the `pom.xml` file. You can use other HTTP clients with the REST producer such as
+
+- camel-http
+- camel-netty4-http
+- camel-jetty
+- camel-restlet
+- camel-undertow
+
+## How to run
+
+You can run this example using
+
+    mvn spring-boot:run
+
+## More information
+
+You can find more information about Apache Camel at the website: http://camel.apache.org/

http://git-wip-us.apache.org/repos/asf/camel/blob/4c8eab4e/examples/camel-example-rest-producer/src/main/java/sample/camel/PetController.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-rest-producer/src/main/java/sample/camel/PetController.java b/examples/camel-example-rest-producer/src/main/java/sample/camel/PetController.java
new file mode 100644
index 0000000..c1234a6
--- /dev/null
+++ b/examples/camel-example-rest-producer/src/main/java/sample/camel/PetController.java
@@ -0,0 +1,43 @@
+/**
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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 sample.camel;
+
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * Used for simulating a rest service which we can run locally inside Spring Boot
+ */
+@RestController
+public class PetController {
+
+    private static String[] PETS = new String[]{"Snoopy", "Fido", "Tony the Tiger"};
+
+    @RequestMapping(value = "/petById/{id}", produces = "application/json")
+    public String petById(@PathVariable("id") Integer id) {
+        if (id != null && id > 0 && id <= PETS.length + 1) {
+            int index = id - 1;
+            String pet = PETS[index];
+            return String.format("{ \"name\": \"%s\" }", pet);
+        } else {
+            // empty pet
+            return "{ }";
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/4c8eab4e/examples/camel-example-rest-producer/src/main/java/sample/camel/RestApplication.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-rest-producer/src/main/java/sample/camel/RestApplication.java b/examples/camel-example-rest-producer/src/main/java/sample/camel/RestApplication.java
new file mode 100644
index 0000000..82020f5
--- /dev/null
+++ b/examples/camel-example-rest-producer/src/main/java/sample/camel/RestApplication.java
@@ -0,0 +1,37 @@
+/**
+ * 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 sample.camel;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+//CHECKSTYLE:OFF
+/**
+ * A Spring Boot application that starts the Camel routes.
+ */
+@SpringBootApplication
+public class RestApplication {
+
+    /**
+     * A main method to start this application.
+     */
+    public static void main(String[] args) {
+        SpringApplication.run(RestApplication.class, args);
+    }
+
+}
+//CHECKSTYLE:ON

http://git-wip-us.apache.org/repos/asf/camel/blob/4c8eab4e/examples/camel-example-rest-producer/src/main/java/sample/camel/RestRoute.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-rest-producer/src/main/java/sample/camel/RestRoute.java b/examples/camel-example-rest-producer/src/main/java/sample/camel/RestRoute.java
new file mode 100644
index 0000000..dc2b2f8
--- /dev/null
+++ b/examples/camel-example-rest-producer/src/main/java/sample/camel/RestRoute.java
@@ -0,0 +1,41 @@
+/**
+ * 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 sample.camel;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.springframework.stereotype.Component;
+
+/**
+ * A Camel route that calls the REST service using a timer
+ * <p/>
+ * Use <tt>@Component</tt> to make Camel auto detect this route when starting.
+ */
+@Component
+public class RestRoute extends RouteBuilder {
+
+    @Override
+    public void configure() throws Exception {
+        // call the embedded rest service from the PetController
+        restConfiguration().host("localhost").port(8080);
+
+        from("timer:hello?period={{timer.period}}")
+            .setHeader("id", simple("${random(1,3)}"))
+            .to("rest:get:petById/{id}")
+            .log("${body}");
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/4c8eab4e/examples/camel-example-rest-producer/src/main/resources/application.properties
----------------------------------------------------------------------
diff --git a/examples/camel-example-rest-producer/src/main/resources/application.properties b/examples/camel-example-rest-producer/src/main/resources/application.properties
new file mode 100644
index 0000000..4db845a
--- /dev/null
+++ b/examples/camel-example-rest-producer/src/main/resources/application.properties
@@ -0,0 +1,25 @@
+#
+# 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.
+#
+
+# the name of Camel
+camel.springboot.name = RestCamel
+
+# properties used in the Camel route and beans
+# --------------------------------------------
+
+# how often to trigger the timer
+timer.period = 2000

http://git-wip-us.apache.org/repos/asf/camel/blob/4c8eab4e/examples/camel-example-rest-producer/src/test/java/sample/camel/RestApplicationTest.java
----------------------------------------------------------------------
diff --git a/examples/camel-example-rest-producer/src/test/java/sample/camel/RestApplicationTest.java b/examples/camel-example-rest-producer/src/test/java/sample/camel/RestApplicationTest.java
new file mode 100644
index 0000000..ecb78a3
--- /dev/null
+++ b/examples/camel-example-rest-producer/src/test/java/sample/camel/RestApplicationTest.java
@@ -0,0 +1,47 @@
+/**
+ * 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 sample.camel;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.NotifyBuilder;
+import org.apache.camel.test.spring.CamelSpringBootRunner;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import static org.junit.Assert.assertTrue;
+
+@RunWith(CamelSpringBootRunner.class)
+@SpringBootTest(classes = RestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
+public class RestApplicationTest {
+
+    @Autowired
+    private CamelContext camelContext;
+
+    @Test
+    public void shouldProduceMessages() throws Exception {
+        // we expect that one or more messages is automatic done by the Camel
+        // route as it uses a timer to trigger
+        NotifyBuilder notify = new NotifyBuilder(camelContext).whenDone(1).create();
+
+        assertTrue(notify.matches(10, TimeUnit.SECONDS));
+    }
+
+}