After a device is connected to IoT Platform, the device submits data to IoT Platform. Then, the data can be forwarded to your server by using the Advanced Message Queuing Protocol (AMQP). This article describes how to configure an AMQP server-side subscription. Your server can receive data from the street lamp by using an AMQP client.

Background information

The following figure shows the procedure of obtaining device messages by configuring an AMQP server-side subscription. AMQP

Procedure

  1. Log on to the IoT Platform console.
  2. Configure a consumer group to consume messages. Your server can obtain the messages by listening to the consumer group.
    1. In the left-side navigation pane, choose Rules > Server-side Subscription and click the Consumer Groups tab.
    2. Click Create Consumer Group.
    3. In the Create Consumer Group dialog box, set the consumer group name to StreetLampConsumerGroup and click OK.
  3. Configure the server-side subscription for the product to which the street lamp device belongs. This allows your server to subscribe to various types of messages under the product.
    1. In the left-side navigation pane, choose Rules > Server-side Subscription.
    2. On the Subscriptions tab of the Server-side Subscription page, click Create Subscription.
    3. In the Create Subscription dialog box, set the parameters and click OK.
      Power bank cabinet-Create Subscription
      Parameter Description
      Product Select StreetLamp.
      Subscription Type Select AMQP.
      Consumer Group Select StreetLampConsumerGroup that is created in the previous step.
      Message Type Select Device Upstream Notification.
  4. Connect the AMQP client with IoT Platform.
    In this example, Java is used for development. We recommend that you use the Apache Qpid JMS client. To download the client and view the instructions, see Qpid JMS 0.57.0.

    In this example, the development environment consists of the following components:

    1. Download the demo package and decompress it.
    2. Open IntelliJ IDEA and import the sample project amqp-demo in the demo package.
      In the pom.xml file, the Maven dependency is added to download the Qpid JMS client.
    3. In the AmqpClient.java file under the src/main/java/com.aliyun.iotx.demo directory, set the parameters to the JMS client with IoT Platform. The following table describes the parameters.
      Parameter Example Description
      accessKey LTAI4GFGQvKuqHJhFa****** 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.
      Note If you use a RAM user, you must attach the AliyunIOTFullAccess permission policy to the user. This policy allows the user to manage IoT Platform resources. Otherwise, the connection with IoT Platform fails. For more information about how to authorize a RAM user, see RAM user access.
      accessSecret iMS8ZhCDdfJbCMeA005sieKe******
      consumerGroupId VWhGZ2QnP7kxWpeSSjt****** The ID of the consumer group that is created in Step 3. You can view the ID on the Public Instance page of the IoT Platform console. Choose Rules > Server-side Subscription, and then click the Consumer Groups tab. Find the consumer group and obtain the ID.
      iotInstanceId "" The ID of the instance. The public instance is used in this example. Specify iotInstanceId = "".
      clientId 12345 The client ID. You must use a unique identifier, such as the UUID, MAC address, or IP address of the client. The client ID must be 1 to 64 characters in length.

      Log on to the IoT Platform console. Choose Rules > Server-side Subscription > Consumer Groups, and click View next to the required consumer group. The Consumer Group Details shows the parameter. This parameter allows you to identify clients.

      host 198426864******.iot-amqp.cn-shanghai.aliyuncs.com The AMQP endpoint.

      ${YourHost} format: ${uid}.iot-amqp.${YourRegionId}.aliyuncs.com.

      • ${uid}: Replace this variable with the ID of your Alibaba Cloud account. You can log on to the IoT Platform console, click the profile picture, and then view the account ID on the Security Settings page.
      • ${YourRegionId}: Replace this variable with the ID of the region where your IoT Platform device resides. For more information about region IDs, see Regions and zones.
    4. After you run the sample code, the following log data is returned. The data indicates that the AMQP client is connected to IoT Platform and can receive messages.
      Note In this example, the Thread.sleep(60 * 1000); code snippet is added to end the program after the program starts and runs for 1 minute. You can set the running time based on your business requirements.
      Running result
For more information about how to configure a server-side subscription to receive device messages, see the following articles: