tencent cloud

TDMQ for RocketMQ

Using the 4.x SDK to Send and Receive Normal Messages for 4.x Clusters

PDF
Modo Foco
Tamanho da Fonte
Última atualização: 2026-01-23 17:16:38

Scenarios

TDMQ for RocketMQ supports SDKs in multiple languages for sending and receiving different types of messages. This document describes how to use the 4.x SDK (taking the Java SDK as an example) to connect to the TDMQ for RocketMQ server for normal message sending and receiving.

Prerequisites

You have created TDMQ for RocketMQ cluster resources.
You have prepared the Linux server and configured the environment by referring to Preparations.

Operation Steps

Step 1: Installing the Java Dependency Library

Introduce related dependencies into the Java project. Taking the Maven project as an example, add the following dependencies to pom.xml:
Note:
The dependency version should be 4.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: Producing Messages

1. Creating a Message Producer

// Instantiate a message producer.
DefaultMQProducer producer = new DefaultMQProducer(
namespace,
groupName,
new AclClientRPCHook(new SessionCredentials(accessKey, secretKey))
// ACL permissions.
);
// Set the NameServer address.
producer.setNamesrvAddr(nameserver);
// Start the producer instance.
producer.start();
Parameter
Description
namespace
Namespace name. You can copy the name from the Namespace tab in the console. If you are using a TDMQ for RocketMQ 4.x general cluster, enter the cluster ID for this parameter.



groupName
Producer group name. You can copy the name from the Group tab on the Cluster Management page in the console.
nameserver
Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console.

secretKey
Role name. You can copy the role name from the SecretKey column on the Role Management page in the console.

accessKey
Role token. You can copy it from the AccessKey column on the Role Management page in the console.

2. Sending Messages

You can send messages by using the following methods: synchronous sending, asynchronous sending, and one-way sending.
Synchronous Sending
Asynchronous 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 a message.
SendResult sendResult = producer.send(msg);
System.out.printf("%s%n", sendResult);
}
Parameter
Description
topic_name
Topic name. You can copy the name from the Topic Management page in the console.

TAG
Tags associated with the message.
// Disable retries on message sending failure.
producer.setRetryTimesWhenSendAsyncFailed(0);
// Set the number of messages to send.
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 successful message sending.
countDownLatch.countDown();
System.out.printf("%-10d OK %s %n", index, sendResult.getMsgId());
}

@Override
public void onException(Throwable e) {
// Logic for failed message sending.
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
Topic name. You can copy the name from the Topic Management page in the console.

TAG
Tags associated with the message.
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 a one-way message.
producer.sendOneway(msg);
}
Parameter
Description
topic_name
Topic name. You can copy the name from the Topic Management page in the console.

TAG
Tags associated with the message.
Note:
For batch sending and other situations, see Demo or Community Documentation.

Step 3: Consuming Messages

1. Creating a Consumer

TDMQ for RocketMQ supports two consumption modes: push and pull.
Pushing a Consumer
Pulling a Consumer
// Instantiate a consumer.
DefaultMQPushConsumer pushConsumer = new DefaultMQPushConsumer(
namespace,
groupName,
new AclClientRPCHook(new SessionCredentials(accessKey, secretKey))); // ACL permissions.
// Set the NameServer address.
pushConsumer.setNamesrvAddr(nameserver);
Parameter
Description
namespace
Namespace name. You can copy the name from the Namespace tab in the console. If you are using a TDMQ for RocketMQ 4.x general cluster, enter the cluster ID for this parameter.



groupName
Producer group name. You can copy the name from the Group tab on the Cluster Management page in the console.
nameserver
Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console.

secretKey
Role name. You can copy the role name from the SecretKey column on the Role Management page in the console.

accessKey
Role token. You can copy it from the AccessKey column on the Role Management page in the console.
// Instantiate a consumer.
DefaultLitePullConsumer pullConsumer = new DefaultLitePullConsumer(
namespace,
groupName,
new AclClientRPCHook(new SessionCredentials(accessKey, secretKey)));
// Set the NameServer address.
pullConsumer.setNamesrvAddr(nameserver);
// Set to start consumption from the first offset.
pullConsumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
Parameter
Description
namespace
Namespace name. You can copy the name from the Namespace tab in the console. If you are using a TDMQ for RocketMQ 4.x general cluster, enter the cluster ID for this parameter.



groupName
Producer group name. You can copy the name from the Group tab on the Cluster Management page in the console.
nameserver
Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console.

secretKey
Role name. You can copy the role name from the SecretKey column on the Role Management page in the console.

accessKey
Role token. You can copy it from the AccessKey column on the Role Management page in the console.
Note:
For more consumption types, see Demo or TDMQ for RocketMQ Official Documentation.

2. Subscribing to a Message

The subscription method varies depending on the consumption mode.
Pushing a Subscription
Pulling a Subscription
// Subscribe to a topic.
pushConsumer.subscribe(topic_name, "*");
// Register a callback implementation class to handle 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 successfully consumed and return an appropriate status.
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
});
// Start the consumer instance.
pushConsumer.start();
Parameter
Description
topic_name
Topic name. You can copy the name from the Topic Management page in the console.

"*"
If the subscription expression is null or uses the * wildcard, it indicates subscribing to all messages. It also supports the format "tag1 || tag2 || tag3" to subscribe 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 messages.
List<MessageExt> messageExts = pullConsumer.poll();
System.out.printf("%s%n", messageExts);
}
} finally {
pullConsumer.shutdown();
}
Parameter
Description
topic_name
Topic name. You can copy the name from the Topic Management page in the console.

"*"
If the subscription expression is null or uses the * wildcard, it indicates subscribing to all messages. It also supports the format "tag1 || tag2 || tag3" to subscribe to multiple types of tags.


Ajuda e Suporte

Esta página foi útil?

comentários