tencent cloud

TDMQ for RocketMQ

Release Notes and Announcements
Release Notes
Announcements
Product Introduction
Introduction and Selection of the TDMQ Product Series
What Is TDMQ for RocketMQ
Strengths
Scenarios
Product Series
Comparison with Open-Source RocketMQ
High Availability
Quotas and Limits
Supported Regions
Basic Concepts
Billing
Billing Overview
Pricing
Billing Examples
Pay-as-you-go Switch to Monthly Subscription (5.x)
Renewal
Viewing Consumption Details
Refund
Overdue Payments
Getting Started
Getting Started Guide
Preparations
Step 1: Creating TDMQ for RocketMQ Resources
Step 2: Using the SDK to Send and Receive Messages (Recommended)
Step 2: Running the TDMQ for RocketMQ Client (Optional)
Step 3: Querying Messages
Step 4: Deleting Resources
User Guide
Usage Process Guide
Configuring Account Permissions
Creating the Cluster
Configuring the Namespace
Configuring the Topic
Configuring the Group
Connecting to the Cluster
Managing Messages
Managing the Cluster
Viewing Monitoring Data and Configuring Alarms
Cross-Cluster Message Replication
Use Cases
Naming Conventions for Common Concepts of TDMQ for RocketMQ
RocketMQ Client Use Cases
RocketMQ Performance Load Testing and Capacity Assessment
Access over HTTP
Client Risk Descriptions and Update Guide
Migration Guide for TencentCloud API Operations Related to RocketMQ 4.x Cluster Roles
Migration Guide
Disruptive Migration
Seamless Migration
Developer Guide
Message Types
Message Filtering
Message Retries
POP Consumption Mode (5.x)
Clustering Consumption and Broadcasting Consumption
Subscription Relationship Consistency
Traffic Throttling
​​API Reference(5.x)
History
API Category
Making API Requests
Topic APIs
Consumer Group APIs
Message APIs
Role Authentication APIs
Hitless Migration APIs
Cloud Migration APIs
Cluster APIs
Data Types
Error Codes
​​API Reference(4.x)
SDK Reference
SDK Overview
5.x SDK
4.x SDK
Security and Compliance
Permission Management
CloudAudit
Deletion Protection
FAQs
4.x Instance FAQs
Agreements
TDMQ for RocketMQ Service Level Agreement
Contact Us

Sending and Receiving Normal Messages

PDF
Mode fokus
Ukuran font
Terakhir diperbarui: 2026-01-23 17:52:24

Scenarios

This document uses the Spring Boot Starter SDK as an example to describe how to send and receive messages through an open-source software development kit (SDK), helping you better understand the complete process of sending and receiving messages.

Prerequisites

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

Operation Steps

Step 1: Adding Dependencies

Add dependencies in pom.xml. It is recommended that the latest version 2.3.3 be used, which supports both Spring Boot 2 and Spring Boot 3.
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.3.3</version>
</dependency>

Step 2: Preparing Configurations

Add configuration information to the configuration file.
server:
port: 8082

#TDMQ for RocketMQ configuration information.
rocketmq:
# TDMQ for RocketMQ service access address.
name-server: rocketmq-xxx.rocketmq.ap-bj.public.tencenttdmq.com:9876
// Producer configuration.
producer:
# Producer group name.
group: group111
# Role token.
access-key: eyJrZXlJZC....
# Authorized role name.
secret-key: admin
# Consumer common configuration.
consumer:
# Role token.
access-key: eyJrZXlJZC....
# Authorized role name.
secret-key: admin

# Custom configuration, which is configured based on the business.
namespace: rocketmq-xxx|namespace1
producer1:
topic: testdev1
consumer1:
group: group111
topic: testdev1
subExpression: TAG1
consumer2:
group: group222
topic: testdev1
subExpression: TAG2
Parameter
Description
name-server
Cluster access address. You can obtain the access address from the Access Information module on the Cluster Basic Information page in the console.
group
Producer name, which is user-defined and can be set to the topic name.
secret-key
Role name. You can copy the role name from the SecretKey column on the Cluster Permissions page in the console.
access-key
Role token. You can copy the token from the AccessKey column on the Cluster Permissions page in the console.
namespace
Namespace name. You can copy the name from the Namespace page in the console. If you are using a 5.x cluster or 4.x general cluster, enter the cluster ID here.
topic
Topic name. You can copy the name from the Topic Management page in the console.
subExpression
Tags associated with the message.

Step 3: Sending Messages

1. Inject RocketMQTemplate in the class that requires message sending.
@Value("${rocketmq.namespace}%${rocketmq.producer1.topic}")
private String topic; // Topic name. (The full topic name is required. Therefore, concatenate the topic name here.)
@Autowired
private RocketMQTemplate rocketMQTemplate;
2. Send messages. The message body can be a custom object or a message object (in the org.springframework.messaging package).
SendResult sendResult = rocketMQTemplate.syncSend(destination, message);
/*------------------------------------------------------------------------*/
rocketMQTemplate.syncSend(destination, MessageBuilder.withPayload(message).build())
3. The following is a complete example:
/**
* Description: Message producer.
*/
@Service
public class SendMessage {
// The full topic name is required. Therefore, concatenate the topic name. You can also set the format yourself: full namespace name%topic name.
@Value("${rocketmq.namespace}%${rocketmq.producer1.topic}")
private String topic;
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* Synchronous sending.
*
* @param message Message content.
* @param tags Tags subscribed to.
*/
public void syncSend(String message, String tags) {
// Spring Boot does not support passing tags using headers. As required, it is necessary to concatenate tags after the topic name in the format of `topicName:tags`. If no concatenation identifier is available, no tag is involved.
String destination = StringUtils.isBlank(tags) ? topic : topic + ":" + tags;
SendResult sendResult = rocketMQTemplate.syncSend(destination,
MessageBuilder.withPayload(message)
.setHeader(MessageConst.PROPERTY_KEYS, "yourKey") // Specify the business key.
.build());
System.out.printf("syncSend1 to topic %s sendResult=%s %n", topic, sendResult);
}
}
Note
This sample demonstrates synchronous sending. For asynchronous sending, one-way sending, and others, see Demo.

Step 4: Consuming Messages

@Service
@RocketMQMessageListener(
consumerGroup = "${rocketmq.namespace}%${rocketmq.consumer1.group}", // Specify the consumer group in the format of full namespace name%group name.
// The full topic name is required. Therefore, concatenate the topic name. You can also set the format yourself: full namespace name%topic name.
topic = "${rocketmq.namespace}%${rocketmq.consumer1.topic}",
selectorExpression = "${rocketmq.consumer1.subExpression}" // Subscription expression. If it is not configured, all messages are subscribed to.
)
public class MessageConsumer implements RocketMQListener<String> {

@Override
public void onMessage(String message) {
System.out.println("Tag1Consumer receive message:" + message);
}
}
Multiple consumers can be configured based on specific business requirements. Other consumer configurations can be configured based on actual business requirements.
Note:
For a complete example, see Downloading the Demo.

Step 5: 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.

Bantuan dan Dukungan

Apakah halaman ini membantu?

masukan