All Products
Search
Document Center

IoT Platform:Create an MNS server-side subscription

Last Updated:Sep 14, 2024

This topic describes how to create a Simple Message Queue (formerly MNS) (SMQ) server-side subscription to push messages about the status changes of devices under a product to an MNS queue. Your server can receive the messages by listening to the MNS queue.

Prerequisites

  • The following Alibaba Cloud services are activated:

  • The development environment is ready for use. In this example, the following Java development environment is used.

Background information

The following figure shows how data flows.

Server-side subscription

Configure a server-side subscription

In the IoT Platform console, create an MNS server-side subscription and select the types of messages to which you want to subscribe.

  1. Log on to the IoT Platform console.

  2. On the Overview page, click All environment. On the All environment tab, find the instance that you want to manage and click the instance ID or instance name.

  3. In the left-side navigation pane, choose Devices > Products. On the Products page, click Create Product to create a product. In this example, a gas detector product is created.

  4. Choose Devices > Add Device to create a device under the gas detector product.

    Before you use Link SDK to develop a device, you must obtain the device certificate.

  5. In the left-side navigation pane, choose Rules Engine > Server-side Subscription. On the Server-side Subscription page, click Create Subscription to create an MNS server-side subscription. For more information, see Configure MNS server-side subscriptions.

    Note

    The first time you select MNS from the Subscription Type drop-down list, a message prompts you to authorize IoT Platform to access MNS. Click Authorize Now to go to the Resource Access Management (RAM) console. Click Confirm Authorization Policy.

    Set the Message Type parameter to Device Status Change Notification. All messages about the status changes of devices under the product are pushed to an MNS queue.

    After you create the subscription, IoT Platform automatically creates a queue in MNS. The queue is used to receive messages from IoT Platform. The format of the queue name is aliyun-iot-${yourProductKey}. When you use MNS SDK to listen to the messages of a queue, you must specify the name of the queue.

    On the Subscriptions tab, move the pointer over the icon next to MNS to view the name of the MNS queue.

    Server-side subscription

Use MNS SDK to receive messages

In this example, MNS SDK for Java is used.

  1. Download the sample package of MNS SDK for Java, and decompress the package. To download the sample package, see Release notes of the SDK for Java.

    In this example, the aliyun-sdk-mns-samples-1.1.9.1.zip package is used.

  2. In IntelliJ IDEA, select the aliyun-sdk-mns-samples-1.1.9.1 directory to import a project.

  3. In the C:\Users\${YourComputerUserName} local directory, create the .aliyun-mns.properties file. Add identity information in the following format to the file. The identity information is used by MNS for authentication.

    Note

    In Linux, the home directory is /home/YOURNAME/. In Windows, the home directory is C:\Users\YOURNAME.

    mns.accountendpoint=http://${your_accountId}.mns.${your_regionId}.aliyuncs.com
    mns.accesskeyid=${your_accesskeyid}
    mns.accesskeysecret=${your_accesskeysecret}

    Parameter

    Description

    accountendpoint

    The endpoint of MNS. In the MNS console, select the region where the MNS queue resides and click Details of the queue to view the endpoint.

    accesskeyid

    The AccessKey ID and AccessKey secret of your Alibaba Cloud account.

    Log on to the IoT Platform console, move the pointer over the profile picture, and then click AccessKey Management to obtain the AccessKey ID and AccessKey secret.

    accesskeysecret

  4. Add the following code to the ComsumerDemo file in the src\main\java\com.aliyun.mns.sample.Queue directory. The code is used to specify the name of the queue that is automatically created by IoT Platform.

        public static void main(String[] args) {
            CloudAccount account = new CloudAccount(
                    ServiceSettings.getMNSAccessKeyId(),
                    ServiceSettings.getMNSAccessKeySecret(),
                    ServiceSettings.getMNSAccountEndpoint());
            MNSClient client = account.getMNSClient(); //Initialize a client.
    
            //Extract messages.
            try{
                CloudQueue queue = client.getQueueRef("aliyun-iot-a1eN7La****");//Specify the name of the queue that is automatically created by IoT Platform.
                for (int i = 0; i < 10; i++)
                {
                    Message popMsg = queue.popMessage(); //Specify the timeout period for long polling.
                    if (popMsg != null){
                        System.out.println("message handle: " + popMsg.getReceiptHandle());
                        System.out.println("message body: " + popMsg.getMessageBodyAsString()); //Obtain raw messages.
                        System.out.println("message id: " + popMsg.getMessageId());
                        System.out.println("message dequeue count:" + popMsg.getDequeueCount());
                        //<<to add your special logic.>>
    
                        //Delete the messages from the queue.
                        queue.deleteMessage(popMsg.getReceiptHandle());
                        System.out.println("delete message successfully.\n");
                    }
                }
            }
  5. Run the ComsumerDemo.java file.

Configure the device SDK

  1. On the Link SDK page, select the "SDK for Java" section.

  2. Download the demo package of Link SDK for Java and decompress the package.

    Note

    By downloading the demo, you agree with the Software License Agreement.

  3. In IntelliJ IDEA, import the JavaLinkKitDemo directory as a project.

  4. In the device_id file, specify information about the device certificate.

    Server-side subscription

  5. In the MqttSample file of the src\main\java\com.aliyun.alink.devicesdk.demo directory, specify the name of the topic to which device data is submitted in the publish section.

    Server-side subscription

  6. In the HelloWorld file of the src\main\java\com.aliyun.alink.devicesdk.demo directory, specify the endpoint of the IoT Platform instance to which you want to connect the device.

    For information about how to obtain the endpoint, see Manage the endpoint of an instance. Endpoint

  7. Run the HelloWorld.java file to connect the device to IoT Platform.

Verify the result

After you run the code, a message is sent to the MNS queue. The message indicates that the device is online. You can use MNS SDK for Java to receive the message and then delete the message from the MNS queue.

The following figure shows how to receive the message and then delete the message.

Server-side subscription