tencent cloud

Cluster usage for 4.x Send and receive ordinary messages with 4.x SDK
Last updated: 2025-07-24 10:23:21
Cluster usage for 4.x Send and receive ordinary messages with 4.x SDK
Last updated: 2025-07-24 10:23:21

Overview

TDMQ for RocketMQ supports multiple language SDKs to send and receive different types of messages. This document uses Java SDK calls as an example to introduce the operation process of sending and receiving ordinary messages by connecting to TDMQ for RocketMQ server via the 4.x SDK.

Prerequisites

You have created the required resources as instructed in Resource Creation and Preparation.

Directions

Step 1. Install the Java dependent library

Introduce dependencies in a Java project and add the following dependencies to the pom.xml file. This document uses a Maven project as an example.
Note:
The dependency version must be v4.9.3 or later.
<!-- in your <dependencies> block -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.9.3</version>
</dependency>

<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-acl</artifactId>
<version>4.9.3</version>
</dependency>

Step 2. Produce messages

1. Create message producers

// Instantiate the message producers
DefaultMQProducer producer = new DefaultMQProducer(
namespace,
groupName,
new AclClientRPCHook(new SessionCredentials(accessKey, secretKey))
// ACL permission
);
// Set the NameServer address
producer.setNamesrvAddr(nameserver);
// Start the producer instances
producer.start();
Parameter
Description
namespace
Namespace name, which can be copied on the Namespace page in the console. If you use a 4.x generic cluster, fill in the cluster ID here.

groupName
Producer group name, which can be copied under the Group tab on the Cluster page in the console.
nameserver
Cluster access address, which can be copied under the Network module on the cluster’s basic information page in the console.

secretKey
Role name, copied from the SecretKey column on the role management page in the console.

accessKey
Role token, copied from the AccessKey column on the role management page in the console.

2. Send messages

Messages can be sent in the sync, async, or one-way mode.
Sync sending
Async sending
One-way sending
for (int i = 0; i < 10; i++) {
// Create a message instance and set the topic and message content
Message msg = new Message(topic_name, "TAG", ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
// Send the message
SendResult sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);
}
Parameter
Description
topic_name
Copy the Topic name from the Topic management page in the console.

tag
A parameter used to set the message tag.
// Disable retry upon sending failures
producer.setRetryTimesWhenSendAsyncFailed(0);
// Set the number of messages to be sent
int messageCount = 10;
final CountDownLatch countDownLatch = new CountDownLatch(messageCount);
for (int i = 0; i < messageCount; i++) {
try {
final int index = i;
// Create a message instance and set the topic and message content
Message msg = new Message(topic_name, "TAG", ("Hello rocketMq " + index).getBytes(RemotingHelper.DEFAULT_CHARSET));
producer.send(msg, new SendCallback() {
@Override
public void onSuccess(SendResult sendResult) {
// Logic for message sending successes
countDownLatch.countDown();
System.out.printf("%-10d OK %s %n", index, sendResult.getMsgId());
}

@Override
public void onException(Throwable e) {
// Logic for message sending failures
countDownLatch.countDown();
System.out.printf("%-10d Exception %s %n", index, e);
e.printStackTrace();
}
});
} catch (Exception e){
e.printStackTrace();
}
}
countDownLatch.await(5, TimeUnit.SECONDS);
Parameter
Description
topic_name
Copy the Topic name from the Topic management page in the console.

tag
A parameter used to set the message tag.
for (int i = 0; i < 10; i++) {
// Create a message instance and set the topic and message content
Message msg = new Message(topic_name, "TAG", ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
Send one-way messages
producer.sendOneway(msg);
}
Parameter
Description
topic_name
Copy the Topic name from the Topic management page in the console.

tag
A parameter used to set the message tag.
Note:
For more information on batch sending or other scenarios, see Demo or RocketMQ documentation.

Step 3. Consume messages

1. Create a consumer

TDMQ for RocketMQ supports two consumption modes: push and pull.
Push consumer
Pull consumer
// Instantiate the consumer
DefaultMQPushConsumer pushConsumer = new DefaultMQPushConsumer(
namespace,
groupName,
new AclClientRPCHook(new SessionCredentials(accessKey, secretKey))); //ACL permission
// Set the NameServer address
pushConsumer.setNamesrvAddr(nameserver);
Parameter
Description
namespace
Namespace name, which can be copied on the Namespace page in the console.If you use a 4.x generic cluster, fill in the cluster ID here.

groupName
Producer group name, which can be copied under the Group tab on the Cluster page in the console.
nameserver
Obtain the cluster access address from the access information module on the console cluster basic information page.

secretKey
Role name, copied from the SecretKey column on the role management page in the console.

accessKey
Role token, copied from the AccessKey column on the role management page in the console.
// Instantiate the consumer
DefaultLitePullConsumer pullConsumer = new DefaultLitePullConsumer(
namespace,
groupName,
new AclClientRPCHook(new SessionCredentials(accessKey, secretKey)));
// Set the NameServer address
pullConsumer.setNamesrvAddr(nameserver);
// Specify the first offset as the start offset for consumption
pullConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
Parameter
Description
namespace
Namespace name, copied from the namespace page in the console. If you use a 4.x generic cluster, fill in the cluster ID here.

groupName
Producer group name, which can be copied under the Group tab on the Cluster page in the console.
nameserver
Obtain the cluster access address from the access information module on the console cluster basic information page.

secretKey
Role name, copied from the SecretKey column on the role management page in the console.

accessKey
Role token, copied from the AccessKey column on the role management page in the console.
Note:
For more consumption mode information, see Demo or RocketMQ documentation.

2. Subscribe to messages

The subscription modes vary by consumption mode.
Push Subscription
Pull Subscription
// Subscribe to a topic
pushConsumer.subscribe(topic_name, "*");
// Register a callback implementation class to process messages pulled from the broker
pushConsumer.registerMessageListener((MessageListenerConcurrently) (msgs, context) -> {
// Message processing logic
System.out.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), msgs);
// Mark the message as being successfully consumed and return the consumption status
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
});
// Start the consumer instance
pushConsumer.start();
Parameter
Description
topic_name
Copy the Topic name from the Topic management page in the console.

"*"
If the subscription expression is left empty or specified as asterisk (*), all messages are subscribed to. tag1 || tag2 || tag3 means subscribing to multiple types of tags.
// Subscribe to a topic
pullConsumer.subscribe(topic_name, "*");
// Start the consumer instance
pullConsumer.start();
try {
System.out.printf("Consumer Started.%n");
while (true) {
// Pull the message
List<MessageExt> messageExts = pullConsumer.poll();
System.out.printf("%s%n", messageExts);
}
} finally {
pullConsumer.shutdown();
}
Parameter
Description
topic_name
Copy the Topic name from the Topic management page in the console.

"*"
If the subscription expression is left empty or specified as asterisk (*), all messages are subscribed to. tag1 || tag2 || tag3 means subscribing to multiple types of tags.

Step 4. View consumption details

After sending the completion message, you will get a message ID. You can query the just-sent message, as well as its details and path, on the console under Message Query > Comprehensive Query.


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

Feedback