This topic describes how to use Message Queue for MQTT SDK for Java to automate message sending and receiving by the MQTT client. This scenario does not involve any data exchanges across cloud services.

Prerequisites

Background information

Automatically sending and subscribing to messages by using an MQTT client is the simplest scenario of Message Queue for MQTT. You can use open-source third-party SDKs that support multiple languages to send and receive messages in this scenario. For more information about supported languages, see Download the SDK.

This topic uses Message Queue for MQTT SDK for Java in an Internet environment as an example to describe how to use an MQTT client to automate message sending and receiving.

mqtt_client_send_receive

As shown in the preceding figure, the MQTT client that you have deployed on the Internet is developed in Java. This client needs to send messages to and receive messages from the Message Queue for MQTT broker in IoT and mobile Internet scenarios. You must embed the code of SDK for Java into the MQTT client code, and configure relevant parameters in the SDK to communicate with the Message Queue for MQTT broker.

Network access

Message Queue for MQTT provides Internet endpoints, classic network endpoints and virtual private cloud (VPC) endpoints. We recommend different types of endpoints based on scenarios:
  • In Internet of Things (IoT) and mobile Internet scenarios, we recommend that you use Internet endpoints to connect your MQTT client to Message Queue for MQTT.
  • Classic network endpoints and VPC endpoints are used only in special scenarios. If your applications are deployed on cloud servers, we recommend that you use a server-side messaging service, such as Message Queue for Apache RocketMQ.
Notice To connect an MQTT client to Message Queue for MQTT with an endpoint, use the domain name rather than the IP address because the IP address can change at any time. The Message Queue for MQTT product team is not liable for any direct or indirect faults or losses arising in the following scenarios:
  • Your MQTT client uses the IP address instead of the domain name for access. The Message Queue for MQTT product team updates the domain name resolution, and the original IP address becomes invalid.
  • A firewall policy on IP addresses has been set in the network where your MQTT client is run. The Message Queue for MQTT product team updates the domain name resolution, and the new IP address is blocked due to the firewall policy.
This topic uses the Internet endpoint of Message Queue for MQTT as an example. For more information about scenarios caparison and message attribute mappings between Message Queue for MQTT and Message Queue for Apache RocketMQ, see the following two topics:

Procedure

The following figure shows the procedure of calling Message Queue for MQTT SDK for Java to send and receive messages.quick_start_no_cross_product

Step 1: Create an instance and obtain the endpoint.

  1. Log on to the Message Queue for MQTT console. In the top navigation bar, select the region where the instance is located.
  2. In the Instances section on the Overview page, click Create Now.
  3. On the buy page, select the desired instance type and click Buy Now.
    You can purchase subscription or pay-as-you-go instances. For more information, see Billing methods. This topic uses a pay-as-you-go instance as an example.
  4. On the Confirm Order page, select the corresponding service agreement, and click Pay.
    This topic uses a pay-as-you-go instance as an example.
  5. Return to the Message Queue for MQTTconsole. In the left-side navigation pane, choose Instances > Instance Information.
  6. In the Endpoint Information section on the Instance Information tab, you can view the endpoint information. This topic uses the Internet endpoint as an example.

Step 2: Create a parent topic.

The MQTT protocol supports multi-level topics. You must create a parent topic in the Message Queue for MQTT console or by calling the OpenAPI. You do not need to create a subtopic. For more information about topics, see Terms. In this tutorial, a parent topic is created in the Message Queue for MQTT console.

  1. In the left-side navigation pane, choose Topics > Create Topic.
  2. In the Create Topic dialog box, set Topic and Description and click OK.
    You can view the topic you create on the Topics page.

Step 3: Create a group ID.

For more information about group IDs, see Terms.

  1. In the left-side navigation pane, choose Groups > Create Group ID.
  2. In the Create Group ID dialog box, set Group ID and click OK.
    You can view the group ID you create in the group ID list.

Step 4: Send and receive messages by using SDK for Java.

  1. Download a third-party open-source SDK for Java at Eclipse Paho Java Client.
  2. Download the demo of Alibaba Cloud Message Queue for MQTT SDK for Java at mqtt-java-demo for reference during code development.
  3. Decompress this demo project package to a specific folder.
  4. In IntelliJ IDEA, import the decompressed files to create a project, and check that the following dependencies are included in the pom.xml file.
    <dependencies>
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.10</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.paho</groupId>
                <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
                <version>1.2.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>4.5.2</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.48</version>
            </dependency>
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-onsmqtt</artifactId>
                <version>1.0.3</version>
            </dependency>
            <dependency>
                <groupId>com.aliyun</groupId>
                <artifactId>aliyun-java-sdk-core</artifactId>
                <version>4.5.0</version>
            </dependency>
        </dependencies>
  5. In the MQ4IoTSendMessageToMQ4IoTUseSignatureMode.java class, configure parameters as instructed in code annotations. Most of these parameters configure the Message Queue for MQTT resources created in Steps 1 to 3. For more information about the sample code, see MQTTSendMessageToMQTT. You can execute the main() function to run the code and send and receive messages.

Verify the result

After the provider sends the message and the consumer subscribes to the message, you can query the trace of the message in the Message Queue for MQTT console to verify whether the message has been sent and received. For more information, see Query a message trace.

References