Forward data to an AMQP server-side subscription consumer group for consumption
AMQP server-side subscriptions retrieve real-time data from all devices under a product. To forward messages from specific devices only, use the data forwarding feature. Data forwarding processes and filters messages through a Resolver script before forwarding them to an AMQP server-side subscription consumer group for consumption by an AMQP client. This topic uses a Thing Specification Language (TSL) model data reporting topic as an example to describe the process of forwarding message data.
How it works
Data forwarding sends messages from a specified topic of all or specific devices under the same product to one or more consumer groups in real time. Each consumer group contains multiple consumers (AMQP clients). When a message arrives at a consumer group, a random consumer receives it. Consumer groups are identified by their IDs. The preceding figure illustrates this process:
-
Consumer: A consumer that is registered with a consumer group using an AMQP software development kit (SDK). It receives messages that IoT Platform forwards to the AMQP consumer group.
-
IoT Platform: You can configure the data source topic, data destination, and Resolver script for data forwarding. Then, you can start the Resolver to forward device messages to the AMQP server-side subscription consumer group.
-
Data source: For more information about supported topic message types, see Data formats (for products and devices other than cloud gateways), Custom topics (for MQTT cloud gateways), Message forwarding topics (for GB/T 32960 cloud gateways), Message forwarding topics (for JT/T 808 cloud gateways), and Message forwarding topics (for SL 651 cloud gateways).
For example:
-
Data Source Topic_ProductA: Forwards messages from all devices under Product A to the consumer group.
-
Data Source Topic_DeviceB: Forwards messages from a specific device, Device B, under Product B to the consumer group.
-
-
Data destination: The AMQP server-side subscription consumer group that receives device data. For more information, see Manage AMQP consumer groups.
-
Resolver script: You can use the
writeAmqp(destinationId, payload, tag)function to forward device data to the consumer group of the AMQP client.For more information about the function, see Functions for forwarding data to data destinations.
-
-
AMQP client: After the Resolver is started, IoT Platform automatically forwards device messages to the AMQP client. Do not subscribe to topics in the AMQP client code. The AMQP client receives messages as long as it is online.
Data forwarding does not support sending messages from an AMQP client to a device. To send instructions to a device, you can call the messaging APIs.
Scenarios
Business server receives device messages: Data forwarding provides a flexible way to forward device messages to an AMQP server-side subscription consumer group.
-
Forward messages from specific devices.
-
Forward messages from specific topics.
-
Filter or process messages before forwarding.
To retrieve real-time messages from all devices under a product, configure an AMQP server-side subscription directly. For more information, see Configure an AMQP server-side subscription.
Prerequisites
-
A consumer group is created to serve as the data forwarding destination. You can use the default consumer group (DEFAULT_GROUP) in IoT Platform or create a new one.
-
A data source is added for the device topic whose messages you want to forward. For example, you can create a data source named DataSource and add the TSL model data reporting topic for a specific device. For more information, see Add a data source for data forwarding.
Limits
-
After an AMQP client establishes a connection, it must immediately send an authentication request. If authentication fails within 15 seconds, the server closes the connection.
-
A single connection of an AMQP client is limited to 1,000 transactions per second (TPS). The TPS limit for message forwarding is determined by the instance's Message Forwarding TPS specification. There is no limit on message size. For more information about the limits on AMQP server-side subscriptions, see Limits on server-side subscriptions.
Step 1: Configure a data destination and a Resolver in IoT Platform
Step 1: Configure the data destination
-
Log on to the IoT Platform console.
On the Overview page, 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 Data Forwarding page, click Go to New Version in the upper-right corner.
NoteIf you have performed this operation before, you are automatically redirected to the new version of the Data Forwarding page.
-
Click the Data destination tab, and then click Create data destination.
-
In the Create Data Destination dialog box, enter a name for the data destination, such as DataPurpose. Configure the parameters as described in the following table and then click OK.
Parameter
Description
Select Operation
Select Publish to AMQP Server-side Subscription Consumer Group.
Consumer Group
Select an existing consumer group as the data forwarding target. Click Create Consumer Group to create a new one.
Step 2: Configure and start the Resolver
-
Create a parser, such as DataParser. For more information, see Step 1: Create a parser.
-
On the Resolver Details page, associate the data source.
-
In the configuration wizard, under Data Source, click Associate Data Source.
-
In the dialog box, select DataSource from the Data Source drop-down list and click OK.
-
-
On the Resolver Details page, you can associate data destinations.
-
In the configuration wizard, click Data Destination. Then, in the upper-right corner of the Data Destination list, click Associate Data Destination.
-
In the dialog box that appears, from the Data Destination drop-down list, select DataPurpose and click OK.
-
In the data destination list, find the Data Destination ID and record it. For example, 1000.
You will need this Data Destination ID in the parsing script.
-
-
On the parser details page, click the Parser Script tab.
-
In the script editor, enter a Resolver script.
The Resolver script uses JavaScript-like syntax. For more information about editing methods, see Script syntax.
To forward data to an AMQP server-side subscription consumer group, use the
writeAmqp(destinationId, payload, tag)function. For more information about the function parameters, see Function list.-
To forward data from all devices under a product without specifying devices:
// Use the payload() function to get the message content reported by the device and convert it to the JSON format. var data = payload("json"); // Directly forward the TSL model data. writeAmqp(1000, data, "debug"); -
To forward messages from only a specific device:
// Use the payload() function to get the message content reported by the device and convert it to the JSON format. var data = payload("json"); // Get the name of the device that reported the message. var dn = deviceName(); // Forward the TSL model data of the specified device. if (dn == 'device01') { writeAmqp(1000, data, "debug"); }
-
-
Click Debug. Follow the on-screen instructions to select a product and device, enter the topic and payload data, and verify that the script is executable.
In the Debug dialog on the Debug Parameters tab, the Topic path ends with
/thing/event/property/post, and the Payload data is in JSON format containingproductKey,gmtCreate,deviceName, anditems(example: Temperature value is 38, Humidity value is 25).The following result indicates that the script ran successfully:
action: transmit to amqp[destinationId=1000] -
Click Publish.
-
Return to the Resolver tab on the Data Forwarding page and click Start for the DataParser resolver.
Step 2: Run the AMQP client
-
Use the AMQP SDK sample provided by Alibaba Cloud IoT Platform. Alibaba Cloud does not provide technical support for self-developed AMQP SDKs.
-
This example uses Java. For examples in other languages, see AMQP client access instructions.
-
This example uses an ECS instance with the Alibaba Cloud Linux operating system as the development environment for the AMQP client:
-
Log on to the ECS instance. For more information about logon methods, see Select an ECS remote connection method.
-
Run the following command to download the demo file.
wget https://linkkit-export.oss-cn-shanghai.aliyuncs.com/amqp/amqp-demo.zip -
Run the following command to decompress the demo file.
unzip amqp-demo.zip -
In the
AmqpClient.javafile in thesrc/main/java/com.aliyun.iotx.demodirectory, modify the AMQP access information as described in the following table.ImportantThe demo code in this example includes code to terminate the program (
Thread.sleep(60 * 1000);). This means the program terminates one minute after it starts successfully. In a real-world scenario, you can set the runtime as needed.Parameter
Description
accessKey
The AccessKey ID and AccessKey secret of an Alibaba Cloud account or a Resource Access Management (RAM) user.
Log on to the IoT Platform console, move the pointer over your profile picture, and then click AccessKey Management to get the AccessKey ID and AccessKey secret.
ImportantTo avoid the security risks of hard coding an AccessKey into your business code, use environment variables to manage your AccessKey.
Set the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in your local operating system to your AccessKey ID and AccessKey secret, respectively.
In the sample code, you can retrieve them using the following methods:
-
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID") -
System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
accessSecret
consumerGroupId
The consumer group ID in the corresponding IoT Platform instance.
Log on to the IoT Platform console. In the corresponding instance, choose to view your consumer group ID.
iotInstanceId
The instance ID. View the ID on the Instance Overview page of the IoT Platform console.
-
If an ID value exists, you must pass this ID value.
-
If there is no Instance Overview page or ID value, pass an empty value, which is
iotInstanceId = "".
clientId
A user-defined client ID, up to 64 characters. Use a unique identifier such as the UUID, MAC address, or IP address of your AMQP client server.
After the AMQP client connects, log on to the IoT Platform console. In the corresponding instance, choose . Click View for the consumer group. The Consumer Group Details page will display this parameter, helping you identify and distinguish clients.
connectionCount
The number of connections for the AMQP client, up to 128. Used to scale out real-time message delivery.
The Consumer Group Details page displays connected clients in the format
${clientId}+"-"+number. The minimum value for the number is 0.host
The AMQP access domain name.
For information about the AMQP access domain name corresponding to
${YourHost}, see View and configure instance endpoint information (Endpoint). -
-
The relevant Maven dependencies have been added to the
pom.xmlfile. In theamqp-demoroot directory, run the following command to reload Maven changes and build the project.mvn clean package -
In the
amqp-demo/targetdirectory, run the following command to run the generated JAR package.java -jar demo-0.0.1-SNAPSHOT-jar-with-dependencies.jar -
After you run the sample code, the following output indicates that the AMQP client has connected to IoT Platform and is receiving messages.
ImportantDevice messages can be received on the server only when the AMQP client is online.
10:42:43.254 [main] INFO com.aliyun.iotx.demo.AmqpClient - amqp demo is started successfully, and will exit after 60s 10:59:46.405 [AmqpProvider :(3):[amqps://iot-cn-6******.amqp.iothub.aliyuncs.com:5671]] DEBUG org.apache.qpid.jms.provider.amqp.AmqpConsumer - Dispatching received message: JmsInboundMessageDispatch { sequence = 1, messageId = 1731508564705******, consumerId = ID:4e6d6f2b-1423-4c44-91ca-37202a******:1:1:1 } 10:59:46.409 [JmsSession [ID:4e6d6f2b-1423-4c44-91ca-37202a******:1:1] delivery dispatcher] DEBUG org.apache.qpid.jms.provider.failover.FailoverProvider - Executing Failover Task: message acknowledge -> JmsInboundMessageDispatch { sequence = 1, messageId = 1731508564705******, consumerId = ID:4e6d6f2b-1423-4c44-91ca-37202******:1:1:1 } ackType: DELIVERED (5) 10:59:46.432 [AmqpProvider :(3):[amqps://iot-cn-6******.amqp.iothub.aliyuncs.com:5671]] DEBUG org.apache.qpid.jms.provider.amqp.AmqpConsumer - Delivered Ack of message: JmsInboundMessageDispatch { sequence = 1, messageId = 1731508564705******, consumerId = ID:4e6d6f2b-1423-4c44-91ca-37202a******:1:1:1 } 10:59:46.441 [JmsSession [ID:4e6d6f2b-1423-4c44-91ca-37202******:1:1] delivery dispatcher] DEBUG org.apache.qpid.jms.provider.failover.FailoverProvider - Executing Failover Task: message acknowledge -> JmsInboundMessageDispatch { sequence = 1, messageId = 1731508564705******, consumerId = ID:4e6d6f2b-1423-4c44-91ca-37202a******:1:1:1 } ackType: ACCEPTED (6) 10:59:46.442 [AmqpProvider :(3):[amqps://iot-cn-6******.amqp.iothub.aliyuncs.com:5671]] DEBUG org.apache.qpid.jms.provider.amqp.AmqpConsumer - Accepted Ack of message: JmsInboundMessageDispatch { sequence = 1, messageId = 1731508564705******, consumerId = ID:4e6d6f2b-1423-4c44-91ca-37202a******:1:1:1 } 10:59:46.452 [pool-1-thread-1] INFO com.aliyun.iotx.demo.AmqpClient - receive message, topic = /g18******/device01/thing/event/property/post, messageId = 1731508564705******, content = {"temperature":10,"humidity":56}The online AMQP clients are displayed in the corresponding consumer group. In
amqp-demo,connectionCount = 4indicates four clients.
What to do next
-
When the AMQP client is offline, subscription messages accumulate. After the client comes back online, IoT Platform pushes the accumulated messages. To discard these messages, you can clear them before the AMQP client goes online.
-
After configuration is complete and the AMQP client receives device data, you can log on to the IoT Platform console and go to the corresponding instance to view message operational logs.
-
On the tab, you can view the log records of device data reporting, data forwarding from IoT Platform to the AMQP client, and acknowledgments (ACKs) returned by the AMQP client. For more information, see Query IoT Platform logs.
-
On the tab, click View in the Actions column of the target consumer group. On the consumer group details page, you can view the message consumption rate, message accumulation, consumption logs, and more. For more information, see View and monitor consumer groups.
-
FAQ
-
How do I implement device connection and status synchronization on IoT Platform?
-
Can hexadecimal data reported by a device be subscribed to through an AMQP server-side subscription?
-
Does one consumer group correspond to one AMQP message queue?
-
Can hexadecimal data reported by a device be subscribed to through an AMQP server-side subscription?
-
Why is there a delay when IoT Platform forwards messages through a consumer group?
-
Why doesn't the device receive messages published by the AMQP client?