Help & Documentation>TDMQ for RocketMQ

Sending and Receiving General Messages

Last updated: 2024-10-14 15:34:41

Scenario

This document describes how to use open-source SDK to send and receive messages by using the SDK for Spring Boot Starter as an example and helps you better understand the message sending and receiving processes.

Preparations

You have completed the Resource Creation and Preparation.

Instructions

Step 1. Add dependencies

Add dependencies to the pom.xml file.
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>

Step 2. Prepare the configuration

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

RocketMQ configuration information
rocketmq:
# Service Access Address for TDMQ-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....
# Authorized Role Name
secret-key: admin
# Common configurations for the consumer
consumer:
# Role token
access-key: eyJrZXlJZC....
# Authorized Role Name
secret-key: admin

Custom Configuration, tailored to business requirements.
namespace: rocketmq-xxx|namespace1
producer1:
topic: testdev1
consumer1:
group: group111
topic: testdev1
subExpression: TAG1
consumer2:
group: group222
topic: testdev1
subExpression: TAG2
Category
Note
name-server
The cluster access address can be obtained from the access address in the operation column of the cluster list on the cluster management page of the console. The access point addresses for the new shared and exclusive clusters can be obtained from the namespace list.
group
Producer group name, which can be copied under the Group tab in the console.
secret-key
Copy the role name from the Role Management page.
access-key
Copy the role key from the Role Management page by clicking on the copy option in the key column.

namespace
Namespace name, which can be copied under the Namespace tab in the console.
topic
Topic name, which can be copied under the Topic tab in the console.
subExpression
A parameter used to set the message tag.

Step 3. Send messages

1. Inject RcoketMQTemplate into the class that needs to send messages.
@Value("${rocketmq.namespace}%${rocketmq.producer1.topic}")
private String topic; // Topic name (the full name of the topic is required, hence the topic name is concatenated here)
@Autowired
private RocketMQTemplate rocketMQTemplate;
2. Send messages. The message body can be a custom object or a message object and 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 Subscription 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 in the format of 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
This example demonstrates synchronous sending. For asynchronous sending, one-way sending, and more, please refer to the Demo or visit the GitHub project.

Step 4. Consume messages

@Service
@RocketMQMessageListener(
consumerGroup = "${rocketmq.namespace}%${rocketmq.consumer1.group}", // Consumer group, formatted as: 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);
}
}
Multiple consumers can be configured based on business needs. Other consumer configurations can be tailored to specific business requirements.
Note
For a comprehensive example, refer to Download Demo or visit the GitHub Project.

Step 5. View consumption details

Log in to the TDMQ Console, navigate to Cluster Management > Group page to view the list of clients connected to the Group. Click View Details in the operation column to view consumer details.