tencent cloud

TDMQ for RocketMQ

문서TDMQ for RocketMQSDK Reference4.x SDKSDK for JavaSending and Receiving Filtered Messages

Sending and Receiving Filtered Messages

PDF
포커스 모드
폰트 크기
마지막 업데이트 시간: 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.



도움말 및 지원

문제 해결에 도움이 되었나요?

피드백