tencent cloud

Feedback

Sending and Receiving Filtered Messages

Last updated: 2023-04-12 11:39:41

    Overview

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

    Prerequisites

    You have created the required resources as instructed in Resource Creation and Preparation.
    You have learned about the sending and receiving process of general messages.
    You have downloaded the demo here or have downloaded one at the GitHub project.

    Directions

    Sending a message

    This process is the same as that of general messages, but you need to concatenate the topic sent by rocketMQTemplate to corresponding tag.
    
    // 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; // 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, topic is TopicTest, tag is TAG1, then the first parameter to call rocketMQTemplate method will be TopicTest:TAG1

    Consuming a message

    Set the selectorExpression field to the corresponding filter tag. In the following code, set rocketmq.consumer1.subExpression to TAG1 to consume the messages of TAG1.
    @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 Tag1Consumer implements RocketMQListener<String> {
    
    @Override
    public void onMessage(String message) {
    System.out.println("Tag1Consumer receive message:" + message);
    }
    }
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support