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.


  • 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).


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:

   <!-- Other acceptors... -->
   <acceptor name="mqtt">tcp://</acceptor>

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

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

Step 3: Write the Java Code

Create a new Java class, such as, 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();
      BlockingConnection connection = mqtt.blockingConnection();
      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)};
      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:

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