Java Demo Usage Guide
This topic uses Java as an example to describe how to connect an MQTTS client to the platform and receive subscribed messages from the platform.
Prerequisites
You have installed IntelliJ IDEA by following the instructions provided in For details about the installation, see Installing IntelliJ IDEA.
Installing IntelliJ IDEA
- Go to the IntelliJ IDEA website to download and install a desired version. The following uses 64-bit IntelliJ IDEA 2019.2.3 Ultimate as an example.
- After the download is complete, run the installation file and install Node.js as prompted.
Importing Sample Code
Establishing a Connection
- Set the access address and authentication parameters in com.iot.mqtt.example.demo.MqttConstants.
// Address for connecting the MQTT client to the platform. Replace it with the MQTT access domain name in "Connection Configuration Parameters". String HOST = "${HOST}"; // Access credential. Replace it with the access credential obtained in "Obtaining the MQTT Access Credential". String ACCESS_KEY = "${accessKey}"; String ACCESS_CODE = "${accessCode}"; // Instance ID. This parameter is mandatory when multiple standard instances are purchased in the same region. String INSTANCE_ID = "${instanceId}"; // Topic for receiving data. Replace it with the topic used for rule action creation. String SUBSCRIBE_TOPIC = "${subscribeTopic}";
For details about the parameters in the demo, see Connection Configuration Parameters.
- Run the sample code (com.iot.mqtt.example.demo.MqttDemo) and check whether the subscription is successful based on the log information. This example does not involve the server certificate verification. For details about how to verify the server certificate, see com.iot.mqtt.example.demo.MqttTlsDemo.
- Successful subscription
Figure 1 Successful subscription
- Failed subscription
- The username or password is incorrect.
Figure 2 Incorrect username or password
- The topic does not exist.
Figure 3 The topic does not exist
- The username or password is incorrect.
- Successful subscription
Receiving Data
After topic subscription, when a device reports data and a rule is triggered, the MQTT client can receive the forwarded data. The following figure shows the logs generated when the forwarded data is received.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot