tencent cloud

TDMQ for RocketMQ

문서TDMQ for RocketMQSDK Reference4.x SDKSDK for JavaSending and Receiving Delayed Messages

Sending and Receiving Delayed Messages

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 2026-01-23 17:52:24

Scenarios

This document uses the Java SDK as an example to describe how to send and receive scheduled messages through an open-source software development kit (SDK).

Prerequisites

You have obtained the client connection parameters as instructed in SDK Overview.

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.4 or later, and 4.9.5 is recommended.
<!-- in your <dependencies> block -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.9.4</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-acl</artifactId>
<version>4.9.4</version>
</dependency>

Step 2: Producing Messages

Creating a Message Producer

// Instantiate a message producer.
DefaultMQProducer producer = new DefaultMQProducer(
groupName,
new AclClientRPCHook(new SessionCredentials(accessKey, secretKey)) // ACL permissions.
);
// Set the NameServer address.
producer.setNamesrvAddr(nameserver);
// Start the producer instance.
producer.start();

Note:
You can log in to the TDMQ for RocketMQ console to obtain the following parameters.
Parameter
Description
groupName
Producer group name. It is recommended that the topic name be used.
accessKey
Role token. You can copy the AccessKey from the AccessKey column on the Cluster Permissions page in the console.
secretKey
Role name. You can copy the SecretKey from the SecretKey column on the Cluster Permissions 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.

Sending Messages

Messages with Fixed Delay Levels

int totalMessagesToSend = 5;
for (int i = 0; i < totalMessagesToSend; i++) {
Message message = new Message(TOPIC_NAME, ("Hello scheduled message " + i).getBytes());
// Set the message delay level.
message.setDelayTimeLevel(5);
// Send messages.
SendResult sendResult = producer.send(message);
System.out.println("sendResult = " + sendResult);
}

Messages with Arbitrary Delay

int totalMessagesToSend = 1;
for (int i = 0; i < totalMessagesToSend; i++) {
Message message = new Message(TOPIC_NAME, ("Hello timer message " + i).getBytes());
// Set the time for sending a message.
long timeStamp = System.currentTimeMillis() + 30000;
// To send a scheduled message, specify the scheduled delivery time. The message will be delivered at the specified time. For example, the message will be delivered at 08:08:08 on August 8, 2022.
// If the specified timestamp is earlier than the current time, the message will be delivered to the consumer immediately.
//Set __STARTDELIVERTIME into the message properties.
message.putUserProperty("__STARTDELIVERTIME", String.valueOf(timeStamp));
// Send messages.
SendResult sendResult = producer.send(message);
System.out.println("sendResult = " + sendResult);
}

Step 3: Consuming Messages

Creating a Consumer

TDMQ for RocketMQ supports two consumption modes: push and pull. The push mode is recommended.
// Instantiate a consumer.
DefaultMQPushConsumer pushConsumer = new DefaultMQPushConsumer(
groupName,
new AclClientRPCHook(new SessionCredentials(accessKey, secretKey))); // ACL permissions.
// Set the NameServer address.
pushConsumer.setNamesrvAddr(nameserver);
Note:
You can log in to the TDMQ for RocketMQ console to obtain the following parameters.
Parameter
Description
groupName
Group name. You can copy the name from the Group Management page in the console.
4.x virtual cluster/exclusive cluster: Concatenate the namespace name in the format of full namespace name%group name, such as MQ_INSTxxx_aaa%GroupTest.
4.x general cluster/5.x cluster: The namespace name does not need to be concatenated. Enter the group name.
accessKey
Role token. You can copy the AccessKey from the AccessKey column on the Cluster Permissions page in the console.
secretKey
Role name. You can copy the SecretKey from the SecretKey column on the Cluster Permissions 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.

Subscribing to Messages

The subscription method varies depending on the consumption mode.
// 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.
4.x virtual/exclusive cluster: Concatenate the namespace name in the format of full namespace name%topic name, such as MQ_INSTxxx_aaa%TopicTest.
4.x general cluster/5.x cluster: The namespace name does not need to be concatenated. Enter the topic name.
"*"
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.

Step 4: Viewing Message Details

After a message is sent, you will receive a message ID (messageID). You can choose Message Query > General Query in the console to query the recently sent message, including the message details and trace.
Note:
The above is a brief introduction to message publishing and subscription. For details, see Demo or TDMQ for RocketMQ Official Documentation.


도움말 및 지원

문제 해결에 도움이 되었나요?

피드백