Communicating Between Quarkus and Mosquitto Containers: A Step-by-Step Guide
In today's post, we will explore how a Quarkus application can communicate with an Eclipse Mosquitto MQTT broker running in Docker containers. We will set up a simple application using Docker Compose, allowing for easy management of our services.
Prerequisites
Before we begin, ensure you have the following:
Docker Desktop installed on your machine.
A zip file of your Quarkus application, which we will unpack and build.
Project Structure
Let’s assume your Quarkus application zip file contains the following structure:
your-quarkus-app.zip
│
├── pom.xml
├── src
│ └── main
│ └── java
│ └── com
│ └── example
│ └── YourApplication.java
└── application.properties
Step 1: Unpack and Build Your Quarkus Application
Unzip your Quarkus application:
unzip your-quarkus-app.zip cd your-quarkus-app
Build your application using Maven:
mvn clean package
This command generates a runnable JAR file in the target
directory.
Step 2: Create the Docker Compose File
Next, create a docker-compose.yaml
file in the root of your project directory:
version: '3.8'
services:
pulsar-broker:
image: eclipse-mosquitto:2
container_name: pulsar-broker
hostname: pulsar-broker
ports:
- "1884:1884"
networks:
- pulsar-network
volumes:
- ./mosquitto.conf:/mosquitto/config/mosquitto.conf
quarkus-app:
image: openjdk:17-jdk-alpine
container_name: quarkus-app
ports:
- "8081:8080"
environment:
- MQTT_HOST=pulsar-broker
networks:
- pulsar-network
volumes:
- ./target/simple-mqtt-1.0.0-SNAPSHOT-runner.jar:/app/application.jar
- ./target/application.properties:/app/config/application.properties
command: >
sh -c "sleep 30 && java -jar /app/application.jar" # Add delay here
working_dir: /app
networks:
pulsar-network:
external: true
driver: bridge
Step 3: Create the Mosquitto Configuration File
Create a file named mosquitto.conf
in the root of your project directory:
# Port on which Mosquitto will listen for connections
listener 1884
# Allow anonymous connections (no authentication required)
allow_anonymous true
bind_address 0.0.0.0
Step 4: Configure Your Quarkus Application
In your application.properties
file, set up the MQTT configuration to connect to the Mosquitto broker:
# MQTT Receiving
mp.messaging.incoming.arconsis-twitch.connector=smallrye-mqtt
mp.messaging.incoming.arconsis-twitch.host=pulsar-broker
mp.messaging.incoming.arconsis-twitch.port=1884
mp.messaging.incoming.arconsis-twitch.topic=arconsis/twitch/#
mp.messaging.outgoing.hello-world.connector=smallrye-mqtt
mp.messaging.outgoing.hello-world.host=pulsar-broker
mp.messaging.outgoing.hello-world.port=1884
mp.messaging.outgoing.hello-world.topic=arconsis/twitch/hello-world
logging.level.io.smallrye.reactive.messaging.mqtt=DEBUG
Step 5: Start the Docker Containers
Run the following command to start the services defined in your Docker Compose file:
docker-compose up -d
This command will start both the Mosquitto broker and the Quarkus application in separate containers.
Step 6: Testing Your Quarkus Application
To test the endpoints of your Quarkus application running in the container, we’ve exposed port 8081
on your host machine. You can use Postman to make requests to your application.
Open Postman.
Set the request type to
GET
.Enter the following URL:
http://localhost:8081/hello/world/HellofromAdarsh
Click Send.
If everything is set up correctly, you should receive a response from your Quarkus application.
Conclusion
In this blog post, we explored how to set up a Quarkus application that communicates with an Eclipse Mosquitto MQTT broker using Docker containers. By leveraging Docker Compose, we simplified the management of our services and demonstrated how to expose and test our application endpoints.
Feel free to extend this setup to explore more complex interactions or deploy it in a production environment. Happy coding!