tencent cloud

Sending and Receiving Normal Messages
Last updated:2026-01-23 17:52:24
Sending and Receiving Normal Messages
Last updated: 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.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback