This topic shows engineers who are unfamiliar with ApsaraMQ for MQTT how to develop a test project for ApsaraMQ 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.

Prerequisites

  • IntelliJ IDEA is installed.

    Make sure that IntelliJ IDEA Ultimate Edition is downloaded and installed as required. For more information, visit the download address. 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.

Run the demo

  1. Download and decompress the demo package.
    To download the demo, visit mqtt-server-sdk-demo. After the package is decompressed, the mqtt-server-sdk-demo-main 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.
    • MQTTProducerDemo class: used by backend service applications to send messages to the ApsaraMQ for MQTT broker.
    • MQTTConsumerDemo class: used by backend service applications to subscribe to messages from the ApsaraMQ for MQTT broker.
    • MQTTClientStatusNoticeProcessDemo class: used by backend service applications to subscribe to the client status notifications.
  2. Import the extracted demo to IntelliJ IDEA.
  3. 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 of the ApsaraMQ for MQTT instance. For information about the format of the endpoint, see Common parameters. 
    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 the value to 5672. 
    int port = "port";
    // The ID of the ApsaraMQ for MQTT instance that you created. 
    String instanceId = "instanceId";
    // The AccessKey ID and AccessKey secret of the Resource Access Management (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 conventions of subtopics, see Terms 
    String secondTopic = "secondTopic";
    // The group ID that you created to send and subscribe to messages. 
    String mqttGroupId = "mqttGroupId";
  4. 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 ApsaraMQ for MQTT console to verify the messaging results. For more information, see Query the message traces.