How to send messes with the MQTT protocol in Artemis

In this tutorial we will discuss about using MQTT connectivity protocol which is an extremely lightweight publish/subscribe messaging transport. For this reason it is useful for connections with remote locations where a small code footprint is required and/or network bandwidth such as pocket devices (e.g. mobile).

In this tutorial, we will explore how to use the MQTT protocol to enable messaging with the Artemis broker. We will provide a step-by-step guide using a Java example to publish and subscribe to MQTT messages.

Prerequisites

  • Basic understanding of messaging concepts
  • Artemis broker installed and running
  • Java Development Kit (JDK) installed

Step 1: Set Up Dependencies

To work with MQTT in Java, we need to add the necessary dependencies to our project. In this tutorial, we will use the org.fusesource.mqtt-client library. Ensure that you have the appropriate dependencies configured in your build system (e.g., Maven or Gradle).

<dependency>
    <groupId>org.fusesource.mqtt-client</groupId>
    <artifactId>mqtt-client</artifactId>
    <version>1.16</version>
</dependency>

Step 2: Enable Connectors and Acceptors in Artemis MQ

Next, you need to enable the MQTT Acceptor and Connector in your broker.xml configuration.

Within the <connectors> section of the broker.xml file, add the following configuration for the MQTT acceptor:

<acceptors>
   <!-- Other acceptors... -->
   <acceptor name="mqtt">tcp://0.0.0.0:1883?protocols=MQTT</acceptor>
</acceptors>

Next, within the <connectors> section, add the following configuration for the MQTT protocol connector:

<connectors>
   <!-- Other connectors... -->
   <connector name="mqtt-connector">tcp://localhost:1883</connector>
</connectors>

Step 3: Write the Java Code

Create a new Java class, such as MQTTPublishSubscribeExample.java, which contains the following code:

package org.apache.activemq.artemis.mqtt.example;

import java.util.concurrent.TimeUnit;

import org.fusesource.mqtt.client.BlockingConnection;
import org.fusesource.mqtt.client.MQTT;
import org.fusesource.mqtt.client.Message;
import org.fusesource.mqtt.client.QoS;
import org.fusesource.mqtt.client.Topic;


public class MQTTPublishSubscribeExample {

   public static void main(final String[] args) throws Exception {
      // Create a new MQTT connection to the broker.  We are not setting the client ID.  The broker will pick one for us.
      System.out.println("Connecting to Artemis using MQTT");
      MQTT mqtt = new MQTT();
      mqtt.setHost("tcp://localhost:1883");
      BlockingConnection connection = mqtt.blockingConnection();
      connection.connect();
      System.out.println("Connected to Artemis");

      // Subscribe to topics
      Topic[] topics = {new Topic("mqtt/example/publish", QoS.AT_LEAST_ONCE), new Topic("test/#", QoS.EXACTLY_ONCE), new Topic("foo/+/bar", QoS.AT_LEAST_ONCE)};
      connection.subscribe(topics);
      System.out.println("Subscribed to topics.");

      // Publish Messages
      String payload1 = "This is message 1";
      String payload2 = "This is message 2";
      String payload3 = "This is message 3";

      connection.publish("mqtt/example/publish", payload1.getBytes(), QoS.AT_LEAST_ONCE, false);
      connection.publish("test/test", payload2.getBytes(), QoS.AT_MOST_ONCE, false);
      connection.publish("foo/1/bar", payload3.getBytes(), QoS.AT_MOST_ONCE, false);
      System.out.println("Sent messages.");

      Message message1 = connection.receive(5, TimeUnit.SECONDS);
      Message message2 = connection.receive(5, TimeUnit.SECONDS);
      Message message3 = connection.receive(5, TimeUnit.SECONDS);
      System.out.println("Received messages.");

      System.out.println(new String(message1.getPayload()));
      System.out.println(new String(message2.getPayload()));
      System.out.println(new String(message3.getPayload()));
   }
}

This example shows the basics of MQTT including how to connect to the Artemis broker and how to send and receive messages including subscriptions using wildcard addresses.

Step 4: Run the Example

Compile and run the Java code. Make sure your Artemis broker is running before executing the program. You should observe the published messages being received and displayed in the console.

Congratulations! You have successfully used the MQTT protocol with the Artemis broker for messaging using the provided Java example. You can now apply this knowledge to build robust and scalable messaging systems.

Source code: https://github.com/apache/activemq-artemis/tree/main/examples/protocols/mqtt/publish-subscribe

Found the article helpful? if so please follow us on Socials