tencent cloud

TDMQ for RocketMQ

Sending and Receiving Filtered Messages

PDF
Modo Foco
Tamanho da Fonte
Última atualização: 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 whole process of sending and receiving filtered messages.

Prerequisites

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

Operation Steps

Sending Messages

Similar to sending normal messages, you only need to concatenate the tag to the sending topic in the rocketMQTemplate method.

// Spring Boot does not support passing tags using headers. As required, tags must be concatenated after the topic in the format of topicName:tags. No concatenation identifier indicates no tags. String destination = StringUtils.isBlank(tags) ? topic : topic + ":" + tags; // Object message type. 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);
For example, if the topic name is TopicTest and the tag is TAG1, the first parameter for calling the rocketMQTemplate method is TopicTest:TAG1.

Consuming Messages

Set the selectorExpression field to the filter tag. For example, in the following code, set rocketmq.consumer1.subExpression to TAG1 to consume messages of TAG1.
@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 Tag1Consumer implements RocketMQListener<String> {

@Override
public void onMessage(String message) {
System.out.println("Tag1Consumer receive message:" + message);
}
}


Ajuda e Suporte

Esta página foi útil?

comentários