This topic describes how to import data from other Alibaba Cloud services to Message Queue for MQTT. It uses the data exchanges of Message Queue for Apache RocketMQ, the only service supported, as an example.

Prerequisites

Background information

This topic uses SDK for Java in an Internet environment as an example to describe how to redirect the data of Message Queue for Apache RocketMQ to 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, see Download the SDK.

quick_start_data_inflow

As shown in the preceding figure, you want to transmit the data of the backend application that you have deployed on the Internet to the MQTT client on the Internet. Both the backend application and the MQTT client are developed in Java. The redirection of the inbound data from Message Queue for Apache RocketMQ to Message Queue for MQTT is implemented by creating and running data inbound rules. The brokers of the two services send messages to and receive messages from their respective clients by using the SDKs for Java provided by the services.

Note Messages cannot be sent between a Message Queue for Apache RocketMQ topic in one region and a Message Queue for MQTT topic in another region. Therefore, all resources in this topic must be created in the Internet region. For more information, see Region-specific topics.

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 sending messages to the MQTT client by the backend application.

rocketmq_send_message_to_mqtt

Step 1: Create a Message Queue for MQTT instance and obtain an 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: Create data inbound rules.

The parameters in the rule must be the same as those of the resources you have created.

  1. In the left-side navigation pane, choose Rules > Create Rule.
  2. On the Create Rule panel, click the Data Inflow tab.
  3. On the Create Rule panel, click the Data Inflow tab.
  4. On the Data Inflow tab, set the following parameters as prompted, and click OK.
    Parameter Example value Description
    Rule ID 111111 The global unique identifier of a rule.
    • It can contain only letters, digits, hyphens (-), and underscores (_) and must contain at least one letter or digit.
    • It must be 3 to 64 characters in length, and excessive characters will be automatically truncated.
    • It cannot be updated after the rule is created.
    Rule Description migrate from rocketmq The description of the rule.
    Status Enable Specifies whether to enable the current rule. Valid values:
    • Enable
    • Disable
    Data Source
    Note Only Message Queue for Apache RocketMQ is supported.
    Cloud Service Message Queue for Apache RocketMQ Specifies the source cloud service whose data flows to Message Queue for MQTT.
    Instance ID MQ_INST_13801563067*****_BbyOD2jQ The ID of the instance from the source cloud service.
    Topic Topic_test The resource key of the specified source cloud service, that is, the topic of Message Queue for Apache RocketMQ. The value Topic_test indicates that data from the Topic_test topic is forwarded to the target topic of the Message Queue for MQTT instance.
    Flow Destination
    Topic TopicA Specifies the target topic of Message Queue for MQTT to which data from other source cloud services is imported.
    You can view the data inbound rule you create in the rule list of Rules.

Step 5: 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.openservices</groupId>
                <artifactId>ons-client</artifactId>
                <version>1.8.5.Final</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>
    Note For more information about the ons-client version, see Release notes.
  5. In the RocketMQSendMessageToMQ4IoT.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 and the resources that you created in Message Queue for Apache RocketMQ. For more information about the sample code, see RocketMQSendMessageToMQ4IoT.java.

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