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 2023/04/14 07:56:42 UTC

[camel-kamelets-examples] 01/02: MQTT example

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

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

commit 819e54f6df820bfb5e7bae11b8c05cf02334b412
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Apr 14 09:54:45 2023 +0200

    MQTT example
---
 jbang/mqtt/README.adoc         | 76 ++++++++++++++++++++++++++++++++++++++++++
 jbang/mqtt/conf/mosquitto.conf |  4 +++
 jbang/mqtt/docker-compose.yml  | 10 ++++++
 jbang/mqtt/mqtt.yaml           | 13 ++++++++
 jbang/mqtt/payload.json        |  3 ++
 jbang/mqtt/start.sh            |  7 ++++
 6 files changed, 113 insertions(+)

diff --git a/jbang/mqtt/README.adoc b/jbang/mqtt/README.adoc
new file mode 100644
index 0000000..99a4451
--- /dev/null
+++ b/jbang/mqtt/README.adoc
@@ -0,0 +1,76 @@
+== MQTT
+
+This example is using Camel to receive MQTT events from an external MQTT broker.
+
+=== Install JBang
+
+First install JBang according to https://www.jbang.dev
+
+When JBang is installed then you should be able to run from a shell:
+
+[source,sh]
+----
+$ jbang --version
+----
+
+This will output the version of JBang.
+
+To run this example you can either install Camel on JBang via:
+
+[source,sh]
+----
+$ jbang app install camel@apache/camel
+----
+
+Which allows to run CamelJBang with `camel` as shown below.
+
+=== How to run
+
+You need to run a MQTT broker such as via Docker, or download and run Apache ActiveMQ Artemis.
+
+To use docker, run the following command
+
+[source,sh]
+----
+docker run -it -p 1883:1883 -p 9001:9001 eclipse-mosquitto
+----
+
+
+Then you can run this example using:
+
+[source,sh]
+----
+$ camel run mqtt.yaml
+----
+
+And then from another terminal (or run the integraiton with `--background` option),
+then send a message to the MQTT broker. This can be done with the help from camel-jbang
+where you can send a message as follows:
+
+[source,sh]
+----
+$ camel cmd send --body=file:payload.json mqtt
+----
+
+This will send a message where the payload (body) is read from the local file named payload.json.
+The message is sent to an existing running Camel integration (named mqtt). Then Camel will
+then send the message to the MQTT broker. So in other words we use Camel as a proxy to send the
+message to the actual MQTT broker.
+
+The Camel integration will then consume the payload and output in the console.
+
+[source,text]
+----
+2023-04-14 08:58:58.676  INFO 62348 --- [calCliConnector] mqtt.yaml:13                        : The temperature is 21
+----
+
+
+=== Help and contributions
+
+If you hit any problem using Camel or have some feedback, then please
+https://camel.apache.org/community/support/[let us know].
+
+We also love contributors, so
+https://camel.apache.org/community/contributing/[get involved] :-)
+
+The Camel riders!
diff --git a/jbang/mqtt/conf/mosquitto.conf b/jbang/mqtt/conf/mosquitto.conf
new file mode 100644
index 0000000..6d3b993
--- /dev/null
+++ b/jbang/mqtt/conf/mosquitto.conf
@@ -0,0 +1,4 @@
+protocol mqtt
+listener 1883
+allow_anonymous true
+socket_domain ipv4
\ No newline at end of file
diff --git a/jbang/mqtt/docker-compose.yml b/jbang/mqtt/docker-compose.yml
new file mode 100644
index 0000000..b228e18
--- /dev/null
+++ b/jbang/mqtt/docker-compose.yml
@@ -0,0 +1,10 @@
+version: '3.8'
+services:
+  mosquitto:
+    image: eclipse-mosquitto
+    ports:
+      - '1883:1883'
+    volumes:
+      - ./conf:/mosquitto/config
+      - ./data:/mosquitto/data
+      - ./log:/mosquitto/log
diff --git a/jbang/mqtt/mqtt.yaml b/jbang/mqtt/mqtt.yaml
new file mode 100644
index 0000000..ca32e98
--- /dev/null
+++ b/jbang/mqtt/mqtt.yaml
@@ -0,0 +1,13 @@
+- route:
+    from:
+      uri: kamelet:mqtt5-source
+      parameters:
+        topic: temperature
+        brokerUrl: tcp://localhost:1883
+      steps:
+        - transform:
+            expression:
+              jq:
+                expression: .value
+        - log:
+            message: The temperature is ${body}
diff --git a/jbang/mqtt/payload.json b/jbang/mqtt/payload.json
new file mode 100644
index 0000000..f650d2f
--- /dev/null
+++ b/jbang/mqtt/payload.json
@@ -0,0 +1,3 @@
+{
+  "value": 21
+}
\ No newline at end of file
diff --git a/jbang/mqtt/start.sh b/jbang/mqtt/start.sh
new file mode 100755
index 0000000..d36bb62
--- /dev/null
+++ b/jbang/mqtt/start.sh
@@ -0,0 +1,7 @@
+#!/bin/sh
+
+docker-compose down
+docker rm -f $(docker ps -a -q)
+docker volume rm $(docker volume ls -q)
+docker-compose up -d
+docker-compose logs -f%
\ No newline at end of file