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

Prerequisites

The ID of the consumer group that has subscribed to the messages of a topic is obtained.

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, add the following dependency to a Maven project to download the Qpid JMS client:

<!-- 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 amqp-demo-en 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, configure the AMQP connection parameters. The following table describes the parameters.
    Modify code
    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 RAM user. This policy allows the RAM 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.

    To view the ID of the consumer group, perform the following steps: Log on to the IoT Platform console and click the card of the instance that you want to manange. Choose Rules Engine > Server-side Subscription > Consumer Groups. The ID is displayed on the Consumer Groups tab.

    iotInstanceId "" The ID of the instance. You can view the ID of the instance on the Overview page in the IoT Platform console.
    • If you have an ID value, you must specify the ID for this parameter.
    • If no Overview or ID is generated for your instance, specify an empty string (iotInstanceId = "") for the parameter.
    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 and click the card of the instance that you want to manage. Choose Rules Engine > Server-side Subscription > Consumer Groups. Find the consumer group that you want to manage and click View in the Actions column. The ID of each client is displayed on the Consumer Group Details page. You can use client IDs to efficiently identify clients.

    connectionCount 4 The number of connections that are enabled on the AMQP client. Maximum value: 64. This parameter is used for 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.

    For more information about the endpoints that you can specify for the ${YourHost} variable, see View the endpoint of an instance.

    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 terminate the program after the program starts and runs for 1 minute. You can specify 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 Description
      topic /***********/******/thing/event/property/post The topic that is used to submit device properties.
      messageId 1324198300680719360 The ID of the message.
      content {"temperature":23,"humidity":21,"time":1604548451951} The content of the message.
    • If the output that is similar to the following log information appears, the AMQP client fails to connect to IoT Platform.

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

      Failed

References

For more information about the error codes that are related to server-side subscription, see Error codes that are related to messages.