tencent cloud

TDMQ for RocketMQ

Sending and Receiving Ordered Messages

PDF
Mode fokus
Ukuran font
Terakhir diperbarui: 2026-01-23 17:36:45
TDMQ for RocketMQ is compatible with the community edition HTTP SDK. If your client previously used the community edition HTTP SDK, you do not need to modify any client code when switching to TDMQ for RocketMQ.

Scenarios

If you use Hypertext Transfer Protocol (HTTP) for message sending and receiving, after introducing the open-source HTTP SDK into your client, TDMQ for RocketMQ allows HTTP-based access over private or public networks.
This document introduces how to use the HTTP SDK (taking the Java SDK as an example) to send and receive messages, so as to help you better understand the complete process of sending and receiving messages.
Note:
Transactional messages are not compatible with HTTP.
If you are using a 4.x cluster, you should specify the protocol type (Transmission Control Protocol (TCP) or HTTP) when creating a consumer group. For more information, see Group Management. Therefore, the same consumer group does not support simultaneous consumption by both TCP and HTTP clients.

Prerequisites

Add the SDK dependency for the corresponding language to the pom.xml file by using Maven.

Retry Mechanisms

HTTP uses a mechanism with a fixed retry interval:
Retry Interval
Maximum Number of Retries
5 minutes.
You can modify the consumer group configuration to customize the maximum number of retries. The default is 16 times.
Note:
If the client acknowledges the message within the retry interval, it indicates that the message has been successfully consumed, and no further retries will occur.
If the client has not acknowledged the message by the end of the retry interval, the client will consume the message again.
The message handle for each consumption is only valid within the retry interval and expires afterward.

Operation Steps

Step 1: Installing the Java Dependency Library

Introduce the community edition HTTP SDK dependency in your Java project.

Step 2: Producing Messages

Creating a Message Producer

// Obtain the client.
MQClient mqClient = new MQClient(endpoint, accessKey, secretKey);

// Obtain the producer of the topic.
MQProducer producer = mqClient.getProducer(namespace, topicName);
Note:
You can log in to the TDMQ for RocketMQ console to obtain the following parameters.
Parameter
Description
endpoint
Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console.
accessKey
Role token. You can copy it from the AccessKey column on the Cluster Permissions page in the console.
secretKey
Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console.
namespace
Namespace name. You can copy the namespace name from the Namespace page in the console. If you are using a 4.x general cluster or a 5.x cluster, specify the cluster ID for this parameter.
topicName
Topic name. You can copy the name from the Topic Management page in the console.

Sending Ordered Messages

try {
for (int i = 0; i < 10; i++) {
TopicMessage pubMsg;
pubMsg = new TopicMessage(
("Hello RocketMQ " + i).getBytes(),
"TAG"
);
// Set the ShardingKey used to partition ordered messages.
pubMsg.setShardingKey(i % 3);
TopicMessage pubResultMsg = producer.publishMessage(pubMsg);
System.out.println("Send mq message success. MsgId is: " + pubResultMsg.getMessageId());
}
} catch (Throwable e) {
System.out.println("Send mq message failed.");
e.printStackTrace();
}
Parameter
Description
TAG
Set the message tag.
ShardingKey
Partitioning field for ordered messages. Messages with the same ShardingKey are routed to the same partition.

Step 3: Consuming Messages

Creating a Consumer

// Obtain the client.
MQClient mqClient = new MQClient(endpoint, accessKey, secretKey);

// Obtain the consumer of the topic.
MQConsumer consumer = mqClient.getConsumer(namespace, topicName, groupName, "TAG");
Note:
You can log in to the TDMQ for RocketMQ console to obtain the following parameters.
Parameter
Description
endpoint
Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console.
accessKey
Role token. You can copy it from the AccessKey column on the Cluster Permissions page in the console.
secretKey
Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console.
namespace
Namespace name. You can copy the namespace name from the Namespace page in the console. If you are using a 4.x general cluster or a 5.x cluster, specify the cluster ID for this parameter.
topicName
Topic name. You can copy the name from the Topic Management page in the console.
groupName
Consumer group name. You can copy the name from the Group Management page in the console.

Subscribing to Messages

do {
List<Message> messages = null;

try {
// Consume messages in order through long round-robin scheduling. The messages obtained may be from different partitions, but the order of messages within a single partition is guaranteed.
// For ordered consumption, if any message within a partition is not successfully acknowledged, messages from the partition will be consumed again.
// For a given partition, consumption of the next batch proceeds only after all messages in the current batch are successfully acknowledged.
// If your business is sensitive to consumption delays, it is highly recommended to pull messages concurrently using multiple threads.
messages = consumer.consumeMessageOrderly(
Integer.parseInt(batchSize),
Integer.parseInt(waitSeconds)
);
} catch (Throwable e) {
e.printStackTrace();
}
if (messages == null || messages.isEmpty()) {
System.out.println(Thread.currentThread().getName() + ": no new message, continue!");
continue;
}

for (Message message : messages) {
System.out.println("Receive message: " + message);
}

{
List<String> handles = new ArrayList<String>();
for (Message message : messages) {
handles.add(message.getReceiptHandle());
}

try {
consumer.ackMessage(handles);
} catch (Throwable e) {
if (e instanceof AckMessageException) {
AckMessageException errors = (AckMessageException) e;
System.out.println("Ack message fail, requestId is:" + errors.getRequestId() + ", fail handles:");
if (errors.getErrorMessages() != null) {
for (String errorHandle :errors.getErrorMessages().keySet()) {
System.out.println("Handle:" + errorHandle + ", ErrorCode:" + errors.getErrorMessages().get(errorHandle).getErrorCode()
+ ", ErrorMsg:" + errors.getErrorMessages().get(errorHandle).getErrorMessage());
}
}
continue;
}
e.printStackTrace();
}
}
} while (true);
Parameter
Description
batchSize
Number of messages pulled in a single batch. It supports up to 16 messages.
waitSeconds
Wait time in round-robin scheduling for a single pull request. It supports up to 30 seconds.


Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan