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 Filtered Messages

PDF
Focus Mode
Font Size
Last updated: 2026-01-23 17:52:24

Scenarios

This document uses the Java SDK as an example to describe how to send and receive filtered messages through an open-source software development kit (SDK). Currently, tag-based filtering and SQL-based filtering are supported.

Prerequisites

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

Using Tags for Message Filtering

The basic code for creating producers and consumers is not described in detail here. You can see Sending and Receiving Normal Messages.
For message production, a tag is required when you construct the message body.
For message consumption, a single tag, an asterisk (*), or multiple tag expressions are required when you subscribe to messages.

Step 1: Producing Messages

Sending Messages

There is no difference between sending a code and a simple message. The main difference is that when you construct the message body, you need to include the tag, and only one tag is allowed.
int totalMessagesToSend = 5;
for (int i = 0; i < totalMessagesToSend; i++) {
Message msg = new Message(TOPIC_NAME, "Tag1", "Hello RocketMQ.".getBytes(StandardCharsets.UTF_8));
// Send messages.
SendResult sendResult = producer.send(msg);
System.out.println("sendResult = " + sendResult);
}

Step 2: Consuming Messages

Subscribing to Messages

// Subscribe to a topic. The following example subscribes to all tags.
pushConsumer.subscribe(topic_name, "*");

//Subscribe to the specified tag.
//pushConsumer.subscribe(TOPIC_NAME, "Tag1");

//Subscribe to multiple tags.
//pushConsumer.subscribe(TOPIC_NAME, "Tag1||Tag2");
// 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();
Note:
You can log in to the TDMQ for RocketMQ console to obtain the following parameters.
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.
Note
The above is a brief introduction to message publishing and subscription. For details, see Demo or TDMQ for RocketMQ Official Documentation.

Using SQL Expressions for Message Filtering

The basic code for creating producers and consumers is not described in detail here. You can see Sending and Receiving Normal Messages.
For message production, the user-defined properties are required when you construct the message body.
For message consumption, the SQL expression is required when you subscribe to messages.

Step 1: Producing Messages

There is no difference between sending a code and a simple message. The main difference is that when you construct the message body, you need to include the custom properties, and multiple properties are allowed.
int totalMessagesToSend = 5;
for (int i = 0; i < totalMessagesToSend; i++) {
Message msg = new Message(TOPIC_NAME,"Hello RocketMQ.".getBytes(StandardCharsets.UTF_8));
msg.putUserProperty("key1","value1");
// Send messages.
SendResult sendResult = producer.send(msg);
System.out.println("sendResult = " + sendResult);
}

Step 2: Consuming Messages

For message consumption, the SQL expression is required when you subscribe to messages. The rest is no different from normal messages.
pushConsumer.subscribe(TOPIC_NAME, MessageSelector.bySql("True"));

// Subscribe to a topic. The following shows the SQL expression for subscribing to a single key (most commonly used).
//pushConsumer.subscribe(TOPIC_NAME, MessageSelector.bySql("key1 IS NOT NULL AND key1='value1'"));

//Subscribe to multiple properties.
//pushConsumer.subscribe(TOPIC_NAME, MessageSelector.bySql("key1 IS NOT NULL AND key2 IS NOT NULL AND key1='value1' AND key2='value2'"));
// 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();
Note
The above is a brief introduction to message publishing and subscription. For details, see Demo or TDMQ for RocketMQ Official Documentation.



Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback