tencent cloud

Sending and Receiving General Messages
Last updated: 2025-07-15 18:32:07
Sending and Receiving General Messages
Last updated: 2025-07-15 18:32:07

Overview

This document describes how to use Spring Boot Starter SDK to send and receive messages and helps you better understand the message sending and receiving processes.

Prerequisites

You have created or prepared the required resources as instructed in Resource Creation and Preparation.
You have installed JDK 1.8 or later.
You have installed Maven 2.5 or later.
You have downloaded the demo or obtained the demo in TencentCloud/rocketmq-demo in GitHub.

Directions

Step 1. Add dependencies

Add dependencies to the pom.xml file. Version 2.3.3 (latest) is recommended, as it simultaneously supports Spring Boot 2 and Spring Boot 3.
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.3.3</version>
<exclusions>
<exclusion>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-acl</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-client</artifactId>
<version>4.9.7</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-acl</artifactId>
<version>4.9.7</version>
</dependency>

Step 2. Prepare configurations

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

# RocketMQ configuration information
rocketmq:
# Service access address of TDMQ for RocketMQ
name-server: rocketmq-xxx.rocketmq.ap-bj.public.tencenttdmq.com:9876
# Producer configurations
producer:
# Producer group name
group: group111
# Role token
access-key: eyJrZXlJZC....
# Name of the authorized role
secret-key: admin
# Common configurations for the consumer
consumer:
# Role token
access-key: eyJrZXlJZC....
# Name of the authorized role
secret-key: admin

# Custom configurations based on business needs
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
Retrieve the cluster access address from the access information module on the console cluster basic information page.

group
Producer name. Users can self-define it or use the corresponding Topic name.
secret-key
Role name, copied from the SecretKey column on the Role Management page in the console.
access-key
Role key, copied from the AccessKey column on the Role Management page in the console.

namespace
Namespace name, copied from the namespace page in the console. If you use a 5.x cluster or 4.x generic cluster, fill in the cluster ID here just.
topic
Copy the topic name from the Topic management page in the console.
subExpression
A parameter used to set the message tag.

Step 3. Send messages

1. Inject RocketMQTemplate into the class that needs to send messages.
@Value("${rocketmq.namespace}%${rocketmq.producer1.topic}")
private String topic; // Full topic name, which needs to be concatenated.
@Autowired
private RocketMQTemplate rocketMQTemplate;
2. Send messages. The message body can be a custom object or a message object that is contained in the package org.springframework.messaging.
SendResult sendResult = rocketMQTemplate.syncSend(destination, message);
/*------------------------------------------------------------------------*/
rocketMQTemplate.syncSend(destination, MessageBuilder.withPayload(message).build())
3. Below is a complete sample.
/**
* Description: Message producer
*/
@Service
public class SendMessage {
// Use the full name of the topic, which can be either customized or concatenated in the format of "full namespace name%topic name".
@Value("${rocketmq.namespace}%${rocketmq.producer1.topic}")
private String topic;
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* Sync sending
*
* @param message Message content
* @param tags Subscribed tags
*/
public void syncSend(String message, String tags) {
// Spring Boot does not support passing tags by using the header. You must concatenate the topic name and tags with a colon ":" as in `topicName:tags`; otherwise, the topic has no tag for identification.
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:
Above is a sync sending sample. For more information on async sending and one-way sending, see the demo or TencentCloud/rocketmq-demo‌ in GitHub.

Step 4. Consume messages

@Service
@RocketMQMessageListener(
consumerGroup = "${rocketmq.namespace}%${rocketmq.consumer1.group}", // Consumer group in the format of "full namespace name%group name"
// Use the full name of the topic, which can be either customized or concatenated in the format of "full namespace name%topic name".
topic = "${rocketmq.namespace}%${rocketmq.consumer1.topic}",
selectorExpression = "${rocketmq.consumer1.subExpression}" // Subscription expression. If this parameter is not configured, it means subscribing to all messages.
)
public class MessageConsumer implements RocketMQListener<String> {

@Override
public void onMessage(String message) {
System.out.println("Tag1Consumer receive message:" + message);
}
}
You can configure multiple consumers as needed. The consumer configurations depend on your business requirements.
Note:
For a complete sample, download the demo or obtain the demo in TencentCloud/rocketmq-demo in GitHub.

Step 5. View consumption details

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


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

Feedback