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 learned about the sending and receiving process of general messages.
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.
String destination = StringUtils.isBlank(tags) ? topic : topic + ":" + tags;
SendResult sendResult = rocketMQTemplate.syncSend(destination,
MessageBuilder.withPayload(message)
.setHeader(MessageConst.PROPERTY_KEYS, "yourKey")
.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}",
topic = "${rocketmq.namespace}%${rocketmq.consumer1.topic}",
selectorExpression = "${rocketmq.consumer1.subExpression}"
)
public class Tag1Consumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
System.out.println("Tag1Consumer receive message:" + message);
}
}
Was this page helpful?