This article describes how to connect an AMQP JMS client to Alibaba Cloud IoT Platform and receive messages from IoT Platform.

Prerequisites

The ID of your consumption group is obtained. You can use the default consumption group (DEFAULT_GROUP) or create a consumption group in IoT Platform. For more information, see Manage consumer groups.

Prepare the development environment

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

Download the Apache Qpid JMS client

To download the client and view the instructions, see Qpid JMS 0.57.0.

In this example, download the Qpid JMS client by adding the following dependency:

<!-- amqp 1.0 qpid client -->
 <dependency>
   <groupId>org.apache.qpid</groupId>
   <artifactId>qpid-jms-client</artifactId>
   <version>0.57.0</version>
 </dependency>
 <!-- util for base64-->
 <dependency>
   <groupId>commons-codec</groupId>
  <artifactId>commons-codec</artifactId>
  <version>1.10</version>
</dependency>

Sample code

  1. Download the demo package and decompress it.
  2. Open IntelliJ IDEA and import the sample project aiot-java-demo in the demo package.
  3. In the AmqpClient.java file under the src/main/java/com.aliyun.iotx.demo directory, specify the AMQP connection parameters. The following table describes the parameters.
    Parameter Example Descripition
    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.

    Log on to the IoT Platform console. Choose Rules > Server-side Subscription > Consumer Groups and view the ID of the required consumer group.

    iotInstanceId "" The ID of the instance. In this example, enter an empty string ("").
    clientId 12345 The ID of the client. We recommend that you 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 page shows this parameter. This parameter allows you to identify clients.

    connectionCount 4 The number of connections that are enabled on the AMQP client. Maximum value: 64. This parameter is used for the scale-out in real-time message pushing scenarios.

    On the Consumer Group Details page, each connected client is displayed in the format of ${clientId}+"-"+ a number. The minimum number is 0.

    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, and move the pointer over the profile picture to view the account ID.
    • ${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.

    For more information, see Connect an AMQP client to IoT Platform.

  4. Run the AmqpClient.java program.
    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.
    • 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. Success
      Parameter Example Descripition
      topic /***********/******/thing/event/property/post The topic that is used to submit device properties.
      messageId 1324198300680719360 The ID of the message.
      content null The content of the message.
    • After you run the code, the following log data may be returned. The data indicates that the AMQP client is not connected to IoT Platform.

      You can check the code or network environment based on logs, solve the problem, and then run the code again.

      Failure