tencent cloud

Sending and Receiving General Messages
Last updated: 2025-07-15 18:35:42
Sending and Receiving General Messages
Last updated: 2025-07-15 18:35:42
TDMQ for RocketMQ is fully compatible with the community edition HTTP SDK. If your client used the community edition HTTP SDK, you can switch to TDMQ for RocketMQ without the need for any client-side code modification.

Overview

If you currently use HTTP protocol for message sending and receiving, after introducing the open-source HTTP SDK in your client, TDMQ for RocketMQ supports user access through private network or public network using HTTP protocol.
This document describes how to use HTTP SDK to send and receive messages by using the SDK for Java as an example and helps you better understand the message sending and receiving processes.
Note:
Currently, transactional message cannot be implemented over HTTP.
If you use a 4.x cluster, when creating a group, the protocol type (TCP or HTTP, for details, see Group Management) needs to be set. Therefore, the same group (consumption group) does not support TCP and HTTP clients consuming simultaneously.

Prerequisites

You have created the required resources as instructed in Resource Creation and Preparation.
You have imported dependencies through Maven and added SDK dependencies of the corresponding programming language in the pom.xml file.

Retry Mechanism

A fixed retry interval is used in HTTP, which can't be customized currently.
Retry Interval
Maximum Number of Retries
5 minutes
You can customize the maximum number of retries by modifying the consumer group configuration. The default is 16.
Note:
If the client acknowledges a message within the retry interval, the message consumption is successful and will not be retried.
If the client doesn’t acknowledge a message after the retry interval has expired, the message will become visible again, and the client will consume it again.
The message handle consumed each time is only valid within the retry interval, and become invalid after that time period.

Directions

Step 1. Install the Java dependent library

Add dependency on the community edition HTTP SDK in the Java project.

Step 2. Produce messages

Creating a message producer

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

// Get the topic producer
MQProducer producer = mqClient.getProducer(namespace, topicName);
Note:
The following parameters require logging in to the TDMQ RocketMQ Console to obtain.
Parameter
Description
endpoint
Get the cluster access address from the access information module on the console cluster basic information page.

accessKey
Role key, copied from the AccessKey column on the Cluster Permission page in the console.

secretKey
Role name, copied from the SecretKey column on the Cluster Permission page in the console.
namespace
Copy the namespace name from the namespace page on the console. If you use a 4.x generic cluster or 5.x cluster, fill in the Cluster ID here.

topicName
Copy the topic name from the Topic page in the console

Sending a message

try {
for (int i = 0; i < 10; i++) {
TopicMessage pubMsg;
pubMsg = new TopicMessage(
("Hello RocketMQ " + i).getBytes(),
"TAG"
);
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.

Step 3. Consume messages

Creating a consumer

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

// Get the topic consumer
MQConsumer consumer = mqClient.getConsumer(namespace, topicName, groupName, "TAG");
Note:
The following parameters require logging in to the TDMQ RocketMQ Console to obtain.
Parameter
Description
endpoint
Get the cluster access address from the access information module on the console cluster basic information page.

accessKey
Role key, copied from the AccessKey column on the Cluster Permission page in the console.

secretKey
Role name, copied from the SecretKey column on the Cluster Permission page in the console.
namespace
Copy the namespace name from the namespace page on the console. If you use a 4.x generic cluster or 5.x cluster, fill in the Cluster ID here.

topicName
Copy the topic name from the Topic page in the console.
groupName
Copy the consumer group name from the Group Management page in the console.


Subscribing to messages

do {
List<Message> messages = null;

try {
// long polling of consumption messages
// Long polling means that if the topic has no messages, the request will wait on the server for 3 seconds. If there is a message that can be consumed within 3 seconds, it will return immediately.
messages = consumer.consumeMessage(
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
The number of messages pulled at a time. Maximum value: 16.
waitSeconds
The polling waiting time for a message pull. Maximum value: 30 seconds.

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback