This topic describes how to create a Message Service (MNS) 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.
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, find the instance that you want to manage and click the instance name to go
to the Instance Details page.
Notice Enterprise Edition instances are available in the China (Shanghai) and Japan (Tokyo) regions. If the Enterprise Edition instances are unavailable in the region that you
select, skip this step.
- 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 . 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.
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.
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 |
- 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.
- 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.
- 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.