All Products
Search
Document Center

ApsaraMQ for MQTT:Run the cloud SDK demo

Last Updated:Mar 11, 2026

Build a test project using the ApsaraMQ for MQTT cloud SDK demo for Java. The demo covers three tasks: sending messages, subscribing to messages, and subscribing to client status notifications.

The cloud SDK connects backend applications to the ApsaraMQ for MQTT broker over Advanced Message Queuing Protocol (AMQP) on port 5672.

Prerequisites

Before you begin, make sure that you have:

Sample classes

The demo project contains three classes:

ClassPurpose
MQTTProducerDemoSends messages to the ApsaraMQ for MQTT broker
MQTTConsumerDemoSubscribes to messages from the ApsaraMQ for MQTT broker
MQTTClientStatusNoticeProcessDemoSubscribes to client online/offline status notifications

Step 1: Download and import the demo

  1. Clone or download the demo from mqtt-server-sdk-demo. After extraction, the mqtt-server-sdk-demo-main folder contains the sample code at:

       java-demo\src\main\java\com\aliyun\openservices\lmq\example
  2. Open IntelliJ IDEA and import the mqtt-server-sdk-demo-main project.

Step 2: Configure connection parameters

Open a sample class and replace the following parameters with your resource information:

// Endpoint of your ApsaraMQ for MQTT instance.
// For the endpoint format, see Common parameters.
String domain = "<your-instance-endpoint>";

// Cloud SDK port. Fixed value: 5672.
int port = 5672;

// Instance ID of your ApsaraMQ for MQTT instance.
String instanceId = "<your-instance-id>";

// AccessKey ID and AccessKey secret of your RAM user.
String accessKey = "<your-access-key-id>";
String secretKey = "<your-access-key-secret>";

// Parent topic created in the console for messaging.
String firstTopic = "<your-parent-topic>";

// Subtopic. No need to create in the console.
// For naming conventions, see Terms.
String secondTopic = "<your-subtopic>";

// Group ID created in the console for messaging.
String mqttGroupId = "<your-group-id>";

The following table describes each parameter:

ParameterDescriptionExample
<your-instance-endpoint>Endpoint of your ApsaraMQ for MQTT instance. For the format, see Common parameters.mqtt-cn-xxx.mqtt.aliyuncs.com
<your-instance-id>Instance ID from the ApsaraMQ for MQTT consolemqtt-cn-xxx
<your-access-key-id>AccessKey ID of your RAM userLTAI5tXxx
<your-access-key-secret>AccessKey secret of your RAM userxXxXxXx
<your-parent-topic>Parent topic created in the consoleTestTopic
<your-subtopic>Subtopic name. Does not need to be created in the console. For naming conventions, see Terms./level2/level3
<your-group-id>Group ID created in the consoleGID_Test

Step 3: Run the demo

After you configure the parameters, right-click the sample class in IntelliJ IDEA and run the main() function.

Verify the results

Query message traces in the ApsaraMQ for MQTT console to confirm that messages were sent and received. For instructions, see Query the message traces.