This is repost of my DZone article - Building an IoT AMQP Backend in Seconds With Rhiot.
The AMQP is becoming widely adopted as the protocol of choice the communication between an IoT gateway and a data center. If you would like to rapidly create the AMQP backend service that can be immediately ready for your gateways and devices, the Rhiot AMQP quickstart will be more than interesting for you.
The AMQP cloudlet quickstart can be used as a base for the fat-jar AMQP microservices (aka cloudlets). If you wanna create a simple backend application capable of exposing AMQP-endpoint and handling the AMQP-based communication, the AMQT cloudlet quickstart is the best way to start your development efforts.
Creating and running the AMQP cloudlet project
In order to create the AMQP cloudlet project execute the following commands:
git clone git@github.com:rhiot/quickstarts.git
cp -r quickstarts/cloudlets/amqp amqp
cd amqp
mvn install
To start the AMQP cloudlet execute the following command:
java -jar target/rhiot-cloudlets-amqp-1.0.0-SNAPSHOT.jar
That is really all you need to expose the AMQP message broker to the external devices and gateways.
You can also build and run it as a Docker image (we love Docker and highly recommend this approach):
TARGET_IMAGE=yourUsername/rhiot-cloudlets-amqp
mvn install docker:build docker:push -Ddocker.image.target=${TARGET_IMAGE}
docker run -it ${TARGET_IMAGE}
AMQP broker
By default AMQP cloudlet quickstart starts embedded ActiveMQ AMQP broker on 5672 port. If you would like to connect your cloudlet application to the external ActiveMQ broker (instead of starting the embedded one), run the cloudlet with the
BROKER_URL
environment variable or system property, for example:java -DBROKER_URL=tcp://amqbroker.example.com:61616 -jar target/rhiot-cloudlets-amqp-1.0.0-SNAPSHOT.jar
...or...
docker run -e BROKER_URL=tcp://amqbroker.example.com:61616 -it yourUsername/rhiot-cloudlets-amqp
Sample chat application
The AMQP cloudlet quickstart is in fact a simple chat application. Clients can send the messages to the chat channel by subscribing to the broker and sending the messages to the
chat
AMQP queue.$ amqp-client -h localhost -p 5672 --topic chat "Hello, this is the IoT device!"
The clients can subscribe to the chat updates by listening on the
chat-updates
AMQP topic - whenever the new message has been sent to the chat channel, the clients registered to the chat-updates
will receive the updated chat history.$ amqp-client -h localhost -p 5672 --subscribe --topic chat-updates
Hello, this is the IoT device!
I just wanted to say hello!
Hello, IoT device. Nice to meet you!
The quickstart also exposes the simple REST API that can be used to read the chat history using the HTTP
GET
request:$ curl http://localhost:8180/chat
Hello, this is the IoT device!
I just wanted to say hello!
Hello, IoT device. Nice to meet you!
Architectural overview
When AMQP cloudlet is started with the embedded ActiveMQ broker, the architecture of the example is the following:
When you connect to the external ActiveMQ broker (using
BROKER_URL
option), the architecture of the example becomes more like the following diagram:The quickstart code
You may be wondering how much code do you need in order to take the advantage of the presented AMQP functionality. Below is all code used by our quickstart to handle the AMQP connectivity:
import io.rhiot.steroids.camel.CamelBootstrap; import java.util.LinkedList; import java.util.List; import static io.rhiot.steroids.activemq.EmbeddedActiveMqBrokerBootInitializer.amqpJmsBridge; import static org.apache.commons.lang3.StringUtils.join; public class ChatCloudlet extends CamelBootstrap { static final Listchat = new LinkedList<>(); @Override public void configure() throws Exception { from(amqpJmsBridge("chat")).process( exchange -> chat.add(exchange.getIn().getBody(String.class)) ).process( exchange -> exchange.getIn().setBody(join(chat, "\n")) ).to(amqpJmsBridge("topic:chat-updates")); } }
And below is the code used to start the REST API mentioned before:
import io.rhiot.steroids.camel.Route; import org.apache.camel.builder.RouteBuilder; import static org.apache.commons.lang3.StringUtils.join; @Route public class ChatRestApiRoutes extends RouteBuilder { @Override public void configure() throws Exception { restConfiguration().component("netty4-http").host("0.0.0.0").port(8180); rest("/chat").get().route().process( exchange -> exchange.getIn().setBody(join(ChatCloudlet.chat, "\n")) ); } }
As you can see, Apache Camel is the first class citizen in the Rhiot world. In order to make Camel connectivity easier, Rhiot comes with the static DSL methods like EmbeddedActiveMqBrokerBootInitializer.amqpJmsBridge which can be used to easily create the Camel endpoints associated with the broker used by the quickstart.
Nice post! I'm entirely new in the rhiot / kura / camel word and I'm taking my first baby steps; so I have just one question: where can I find the amqp-client command / tool? Thanks in advance!
ReplyDeleteHi,
ReplyDeleteI'm glad you like it :) .
amqp-client actually doesn't exist. You should replace it with a real AMQP client of your choice.
Cheers!
I see, thank you for the answer!
ReplyDeleteAll the best!