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:
IntelliJ IDEA Ultimate Edition or Eclipse installed
JDK 1.8 or later installed
An ApsaraMQ for MQTT instance, topic, and group created in the console
An AccessKey pair (AccessKey ID and AccessKey secret) for a Resource Access Management (RAM) user
The endpoint of your ApsaraMQ for MQTT instance
Sample classes
The demo project contains three classes:
| Class | Purpose |
|---|---|
MQTTProducerDemo | Sends messages to the ApsaraMQ for MQTT broker |
MQTTConsumerDemo | Subscribes to messages from the ApsaraMQ for MQTT broker |
MQTTClientStatusNoticeProcessDemo | Subscribes to client online/offline status notifications |
Step 1: Download and import the demo
Clone or download the demo from mqtt-server-sdk-demo. After extraction, the
mqtt-server-sdk-demo-mainfolder contains the sample code at:java-demo\src\main\java\com\aliyun\openservices\lmq\exampleOpen IntelliJ IDEA and import the
mqtt-server-sdk-demo-mainproject.
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:
| Parameter | Description | Example |
|---|---|---|
<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 console | mqtt-cn-xxx |
<your-access-key-id> | AccessKey ID of your RAM user | LTAI5tXxx |
<your-access-key-secret> | AccessKey secret of your RAM user | xXxXxXx |
<your-parent-topic> | Parent topic created in the console | TestTopic |
<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 console | GID_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.