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.
Operating system: Windows 10 (64-bit)
Java Development Kit (JDK): JDK 8
Integrated development environment (IDE): IntelliJ IDEA Community Edition
Background information
The following figure shows how data flows.
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.
Log on to the IoT Platform console.
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.
In the left-side navigation pane, choose . On the Products page, click Create Product to create a product. In this example, a gas detector product is created.
Choose to create a device under the gas detector product.
Before you use Link SDK to develop a device, you must obtain the device certificate.
In the left-side navigation pane, choose Configure MNS server-side subscriptions. . On the Server-side Subscription page, click Create Subscription to create an MNS server-side subscription. For more information, see
NoteThe 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.
Use MNS SDK to receive messages
In this example, MNS SDK for Java is used.
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.
In IntelliJ IDEA, select the aliyun-sdk-mns-samples-1.1.9.1 directory to import a project.
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.
NoteIn 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
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"); } } }
Run the ComsumerDemo.java file.
Configure the device SDK
On the Link SDK page, select the "SDK for Java" section.
Download the demo package of Link SDK for Java and decompress the package.
NoteBy downloading the demo, you agree with the Software License Agreement.
In IntelliJ IDEA, import the JavaLinkKitDemo directory as a project.
In the device_id file, specify information about the device certificate.
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.
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.
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.