You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2020/04/30 13:03:23 UTC

[camel-examples] branch master updated: doc(mongodb): adding a MongoDB component example

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-examples.git


The following commit(s) were added to refs/heads/master by this push:
     new 8373ca0  doc(mongodb): adding a MongoDB component example
     new 27fbf34  Merge pull request #3 from squakez/master
8373ca0 is described below

commit 8373ca099cd3102cccd67c57619e7555f0787457
Author: Pasquale Congiusti <pa...@gmail.com>
AuthorDate: Thu Apr 30 12:01:25 2020 +0200

    doc(mongodb): adding a MongoDB component example
---
 examples/README.adoc                               |  4 +-
 examples/camel-example-mongodb/README.adoc         | 40 ++++++++++
 .../docker/docker-compose.yml                      |  8 ++
 examples/camel-example-mongodb/pom.xml             | 93 ++++++++++++++++++++++
 .../apache/camel/example/mongodb/Application.java  | 46 +++++++++++
 .../mongodb/MongoDBFindAllRouteBuilder.java        | 33 ++++++++
 .../mongodb/MongoDBFindByIDRouteBuilder.java       | 33 ++++++++
 .../example/mongodb/MongoDBInsertRouteBuilder.java | 31 ++++++++
 examples/pom.xml                                   |  1 +
 9 files changed, 288 insertions(+), 1 deletion(-)

diff --git a/examples/README.adoc b/examples/README.adoc
index 849e5a0..caad6d7 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: 84 (0 deprecated)
+Number of Examples: 85 (0 deprecated)
 
 [width="100%",cols="4,2,4",options="header"]
 |===
@@ -68,6 +68,8 @@ Number of Examples: 84 (0 deprecated)
 
 | link:camel-example-jooq/README.adoc[JOOQ] (camel-example-jooq) | Database | An example for showing Camel using JOOQ component
 
+| link:camel-example-mongodb/README.adoc[Mongodb] (camel-example-mongodb) | Database | An example that uses Camel MongoDB component
+
 | link:camel-example-mybatis/README.adoc[MyBatis] (camel-example-mybatis) | Database | An example for showing Camel using MyBatis SQL mapper component
 
 | link:camel-example-aggregate/README.adoc[Aggregate] (camel-example-aggregate) | EIP | Demonstrates the persistent support for the Camel aggregator
diff --git a/examples/camel-example-mongodb/README.adoc b/examples/camel-example-mongodb/README.adoc
new file mode 100644
index 0000000..90bc75a
--- /dev/null
+++ b/examples/camel-example-mongodb/README.adoc
@@ -0,0 +1,40 @@
+== Camel MongoDB Example
+
+This example shows how to use Camel MongoDB component. There are three REST endpoints that will trigger the MongoDB component for reading and for writing.
+
+=== Run a Mongo instance
+
+You need to have an instance of MongoDB server running locally. You can find a docker-compose file under `/docker/` directory.
+
+    cd docker
+    docker-compose up
+
+=== Run the Camel integration
+
+You can now run your application by executing:
+
+    mvn exec:java
+
+=== Make some test call
+
+You can insert an "hello" document by POSTing to `/hello` endpoint:
+
+    curl -X POST -H "Content-Type: application/json" -d '{"text":"Hello from Camel"}' http://localhost:8081/hello
+
+You can read all the documents by requesting to `/` endpoint:
+
+    curl localhost:8081
+
+You can also read a single document by providing the `id` parameter:
+
+    curl localhost:8081/hello?id=5eaa94933aff184354c4a874
+
+=== Help and contributions
+
+If you hit any problem using Camel or have some feedback, then please
+https://camel.apache.org/support.html[let us know].
+
+We also love contributors, so
+https://camel.apache.org/contributing.html[get involved] :-)
+
+The Camel riders!
diff --git a/examples/camel-example-mongodb/docker/docker-compose.yml b/examples/camel-example-mongodb/docker/docker-compose.yml
new file mode 100644
index 0000000..aa5b5f8
--- /dev/null
+++ b/examples/camel-example-mongodb/docker/docker-compose.yml
@@ -0,0 +1,8 @@
+version: '3.1'
+
+services:
+
+  mongo:
+    image: mongo
+    ports:
+      - 27017:27017
\ No newline at end of file
diff --git a/examples/camel-example-mongodb/pom.xml b/examples/camel-example-mongodb/pom.xml
new file mode 100644
index 0000000..3ec7429
--- /dev/null
+++ b/examples/camel-example-mongodb/pom.xml
@@ -0,0 +1,93 @@
+<?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.example</groupId>
+        <artifactId>examples</artifactId>
+        <version>3.3.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>camel-example-mongodb</artifactId>
+    <name>Camel :: Example :: MongoDB</name>
+    <description>An example that uses Camel MongoDB component</description>
+    <packaging>jar</packaging>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <category>Database</category>
+    </properties>
+
+    <dependencyManagement>
+        <dependencies>
+            <!-- Add Camel BOM -->
+            <dependency>
+                <groupId>org.apache.camel</groupId>
+                <artifactId>camel-bom</artifactId>
+                <version>${camel.version}</version>
+                <type>pom</type>
+                <scope>import</scope>
+            </dependency>
+        </dependencies>
+    </dependencyManagement>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-main</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-management</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-mongodb</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-jetty</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <version>${slf4j-version}</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>exec-maven-plugin</artifactId>
+                <version>${exec-maven-plugin-version}</version>
+                <configuration>
+                    <mainClass>org.apache.camel.example.mongodb.Application</mainClass>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+</project>
diff --git a/examples/camel-example-mongodb/src/main/java/org/apache/camel/example/mongodb/Application.java b/examples/camel-example-mongodb/src/main/java/org/apache/camel/example/mongodb/Application.java
new file mode 100644
index 0000000..4983c21
--- /dev/null
+++ b/examples/camel-example-mongodb/src/main/java/org/apache/camel/example/mongodb/Application.java
@@ -0,0 +1,46 @@
+/*
+ * 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.mongodb;
+
+import com.mongodb.client.MongoClients;
+import org.apache.camel.main.Main;
+
+
+public final class Application {
+
+    private Main main;
+
+    private Application() {
+        // noop
+    }
+
+    public static void main(String[] args) throws Exception {
+        Application app = new Application();
+        app.start();
+    }
+
+    private void start() throws Exception {
+        main = new Main();
+        // bind connectionBean used by the component
+        main.bind("myDb", MongoClients.create("mongodb://localhost"));
+        main.addRoutesBuilder(new MongoDBFindByIDRouteBuilder());
+        main.addRoutesBuilder(new MongoDBFindAllRouteBuilder());
+        main.addRoutesBuilder(new MongoDBInsertRouteBuilder());
+        System.out.println("Starting Camel. Use CTRL + C to terminate the process.\n");
+        main.run();
+    }
+}
diff --git a/examples/camel-example-mongodb/src/main/java/org/apache/camel/example/mongodb/MongoDBFindAllRouteBuilder.java b/examples/camel-example-mongodb/src/main/java/org/apache/camel/example/mongodb/MongoDBFindAllRouteBuilder.java
new file mode 100644
index 0000000..f6dda97
--- /dev/null
+++ b/examples/camel-example-mongodb/src/main/java/org/apache/camel/example/mongodb/MongoDBFindAllRouteBuilder.java
@@ -0,0 +1,33 @@
+/*
+ * 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.mongodb;
+
+import com.mongodb.client.model.Filters;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mongodb.MongoDbConstants;
+import org.bson.types.ObjectId;
+
+public class MongoDBFindAllRouteBuilder extends RouteBuilder {
+
+    @Override
+    public void configure() {
+        from("jetty:http://0.0.0.0:8081/?httpMethodRestrict=GET")
+                .log("Called findAll API")
+                .to("mongodb:myDb?database=test&collection=test&operation=findAll")
+                .setBody(simple("${body}"));
+    }
+}
diff --git a/examples/camel-example-mongodb/src/main/java/org/apache/camel/example/mongodb/MongoDBFindByIDRouteBuilder.java b/examples/camel-example-mongodb/src/main/java/org/apache/camel/example/mongodb/MongoDBFindByIDRouteBuilder.java
new file mode 100644
index 0000000..fc933fd
--- /dev/null
+++ b/examples/camel-example-mongodb/src/main/java/org/apache/camel/example/mongodb/MongoDBFindByIDRouteBuilder.java
@@ -0,0 +1,33 @@
+/*
+ * 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.mongodb;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.bson.types.ObjectId;
+
+public class MongoDBFindByIDRouteBuilder extends RouteBuilder {
+
+    @Override
+    public void configure() {
+        from("jetty:http://0.0.0.0:8081/hello?httpMethodRestrict=GET")
+                .log("Called findById API")
+                .setBody(header("id"))
+                .convertBodyTo(ObjectId.class)
+                .to("mongodb:myDb?database=test&collection=test&operation=findById")
+                .setBody(simple("${body}"));
+    }
+}
diff --git a/examples/camel-example-mongodb/src/main/java/org/apache/camel/example/mongodb/MongoDBInsertRouteBuilder.java b/examples/camel-example-mongodb/src/main/java/org/apache/camel/example/mongodb/MongoDBInsertRouteBuilder.java
new file mode 100644
index 0000000..596cb14
--- /dev/null
+++ b/examples/camel-example-mongodb/src/main/java/org/apache/camel/example/mongodb/MongoDBInsertRouteBuilder.java
@@ -0,0 +1,31 @@
+/*
+ * 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.mongodb;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.bson.types.ObjectId;
+
+public class MongoDBInsertRouteBuilder extends RouteBuilder {
+
+    @Override
+    public void configure() {
+        from("jetty:http://0.0.0.0:8081/hello?httpMethodRestrict=POST")
+                .log("Called insert API")
+                .to("mongodb:myDb?database=test&collection=test&operation=insert")
+                .setBody(simple("${body}"));
+    }
+}
diff --git a/examples/pom.xml b/examples/pom.xml
index 4daf0fd..58a508e 100644
--- a/examples/pom.xml
+++ b/examples/pom.xml
@@ -131,6 +131,7 @@
         <module>camel-example-management</module>
         <module>camel-example-micrometer</module>
         <module>camel-example-mybatis</module>
+        <module>camel-example-mongodb</module>
         <module>camel-example-netty-custom-correlation</module>
         <module>camel-example-netty-http</module>
         <module>camel-example-olingo4-blueprint</module>