This topic shows engineers who are unfamiliar with Message Queue for MQTT how to develop a test project for Message Queue for MQTT in the cloud from scratch by using the cloud SDK demo. In this example, the Java language is used. The demo contains the sample code for sending and subscribing to messages and subscribing to client status notifications. The demo also contains related configurations.
- IntelliJ IDEA is installed.
Make sure that IntelliJ IDEA Ultimate Edition is downloaded and installed as required. For more information, visit Downloads. Alternatively, you can install Eclipse. In this example, IntelliJ IDEA is installed.
- JDK 1.8 or later is installed. For more information, visit Java Downloads.
- Resources are created.
To use the demo to send and subscribe to messages, you must create resources such as an instance, a topic, and a group in advance in the console. In addition, you must obtain the AccessKey pair and the endpoint of the instance in advance.Notice To use the cloud SDK to send and subscribe to messages, you must submit a ticket to obtain the endpoint and configure the endpoint in the code.
Run the demo
- Download and decompress the demo package.
To download the demo, visit mqtt-server-sdk-demo. After the package is decompressed, the server-sdk-demo folder is generated. The sample code for sending and subscribing to messages and subscribing to client status notifications is stored in the java-demo\src\main\java\com\aliyun\openservices\lmq\example directory.
MQTTProducerDemoclass: used by backend service applications to send messages to the Message Queue for MQTT broker.
MQTTConsumerDemoclass: used by backend service applications to subscribe to messages from the Message Queue for MQTT broker.
MQTTClientStatusNoticeProcessDemoclass: used by backend service applications to subscribe to the client status notifications.
- Import the decompressed demo to IntelliJ IDEA.
- Modify the sample code based on your business requirements. You need to set the parameters
in the sample code based on the actual resource information.
// The endpoint for accessing the Message Queue for MQTT instance by using the cloud SDK. You must to obtain the endpoint. String domain = "domain"; // The port that is used by the cloud SDK. The protocol and port that are used by the cloud SDK must match. Set this parameter to 5672. int port = "port"; // The ID of the Message Queue for MQTT instance that you created. String instanceId = "instanceId"; // The AccessKey ID and AccessKey secret of the RAM user. String accessKey = "accessKey"; String secretKey = "secretKey"; // The parent topic that you created for sending and subscribing to messages. String firstTopic = "firstTopic"; // The subtopic to be used. You can specify a subtopic in the code without the need to create it in advance in the console. For more information about the naming format of subtopics, see Terms. String secondTopic = "secondTopic"; // The group ID that you created to send and subscribe to messages. String mqttGroupId = "mqttGroupId";
- After the configuration is complete, execute the main() function to run the modified sample code.
Verify the results
After you run the demo to send and subscribe to messages, you can query message traces in the Message Queue for MQTT console to verify the messaging results. For more information, see Query the message traces.