Deploying a Quarkus Application with Mosquitto MQTT Broker: A Step-by-Step Guide

In this article, we'll walk you through deploying a Quarkus application that utilizes an MQTT broker via Mosquitto. This guide provides detailed instructions for setting up your environment, creating necessary configuration files, and running your application successfully. Let’s get started!

Prerequisites

Before diving into the setup, ensure you have the following installed on your machine:

  1. Java Development Kit (JDK) - Make sure JDK 11 or above is installed.

  2. Apache Maven - This is essential for building your Quarkus application.

  3. Docker - You will need Docker to run the Mosquitto broker.

  4. Postman - For testing your application's API endpoints.

Step 1: Prepare Your Quarkus Application

  1. Unzip Your Quarkus Application
    Begin by unzipping your Quarkus application zip file. You should see the project structure within the unzipped folder.

  2. Open IntelliJ
    Launch IntelliJ IDEA and open the unzipped Quarkus project.

  3. Go to application.properties and paste the below configuration. (Can be found at src/main/resources/application.properties)

     # MQTT Receiving
     mp.messaging.incoming.arconsis-twitch.connector=smallrye-mqtt
     mp.messaging.incoming.arconsis-twitch.host=localhost
     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=localhost
     mp.messaging.outgoing.hello-world.port=1884
     mp.messaging.outgoing.hello-world.topic=arconsis/twitch/hello-world
    
  4. Build the Application
    Open the terminal in IntelliJ and execute the following commands:

     mvn clean
     mvn clean package
    

    These commands will compile your application and create a runnable JAR file. Upon completion, the JAR file can be found at:

     target/simple-mqtt-1.0.0-SNAPSHOT-runner.jar
    

Step 2: Prepare Your Environment

  1. Create a New Directory
    Open a terminal and create a new directory where you will store your JAR file and configuration files:

     mkdir mqtt-setup
     cd mqtt-setup
    
  2. Copy the JAR File
    Copy the JAR file from the target directory into the new directory:

     cp path/to/your/project/target/simple-mqtt-1.0.0-SNAPSHOT-runner.jar .
    

Step 3: Create the Docker Compose File

  1. Create a docker-compose.yml File
    In your terminal, use a text editor to create a docker-compose.yml file:

     vi docker-compose.yml
    

    Add the following contents to the file:

     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
     networks:
       pulsar-network:
         driver: bridge
    

    Save and exit the editor.

Step 4: Configure Mosquitto

  1. Create the Mosquitto Configuration File
    Create a configuration file named mosquitto.conf in the same directory:

     vi mosquitto.conf
    

    Add the following configuration to the file:

     # 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
    

    Save and exit the editor.

Step 5: Run the Mosquitto Container

  1. Start the Mosquitto Broker
    Back in your terminal, execute the following command to run the Mosquitto container:

     docker-compose up -d
    

    This command starts the Mosquitto broker in detached mode.

Step 6: Run Your Quarkus Application

  1. Run the JAR File
    In the terminal, execute the command to start your Quarkus application:

     java -jar simple-mqtt-1.0.0-SNAPSHOT-runner.jar
    

    Your Quarkus application should now be running and ready to receive messages.

Step 7: Test the Application with Postman

  1. Open Postman
    Launch Postman to send a request to your application.

  2. Send a GET Request
    In Postman, create a new GET request with the following URL:

     http://pulsar-network:1884/hello/world/MessageFromPostman
    

    Execute the request.

  3. Check the Command Line
    Switch to the terminal where your Quarkus application is running. You should see the received message logged, confirming that everything is working as expected.

Conclusion

You have successfully deployed a Quarkus application that interacts with a Mosquitto MQTT broker. This setup can serve as a foundation for more complex messaging applications. By following these steps, you can ensure that your application communicates efficiently using the MQTT protocol. Happy coding!

Feel free to reach out if you have any questions or need further assistance!