pom.xml
file.<dependency><groupId>org.apache.rocketmq</groupId><artifactId>rocketmq-spring-boot-starter</artifactId><version>2.2.2</version></dependency>
server:port: 8082# RocketMQ configuration informationrocketmq:# Service access address of TDMQ for RocketMQname-server: rocketmq-xxx.rocketmq.ap-bj.public.tencenttdmq.com:9876# Producer configurationsproducer:# Producer group namegroup: group111# Role tokenaccess-key: eyJrZXlJZC....# Name of the authorized rolesecret-key: admin# Common configurations for the consumerconsumer:# Role tokenaccess-key: eyJrZXlJZC....# Name of the authorized rolesecret-key: admin# Custom configurations based on business needsnamespace: rocketmq-xxx|namespace1producer1:topic: testdev1consumer1:group: group111topic: testdev1subExpression: TAG1consumer2:group: group222topic: testdev1subExpression: TAG2
Parameter | Description |
name-server | Cluster access address, which can be obtained from Access Address in the Operation column on the Cluster page in the console. The namespace access address can be obtained under the Namespace tab on the Cluster page. |
group | Consumer group name, which can be copied under the Group tab on the Cluster page in the console. |
secret-key | |
access-key | |
namespace | Namespace name, which can be copied under the Namespace tab on the Cluster page in the console. |
topic | Topic name, which can be copied under the Topic tab on the Cluster page in the console. |
subExpression | A parameter used to set the message tag. |
RcoketMQTemplate
into the class that needs to send messages.@Value("${rocketmq.namespace}%${rocketmq.producer1.topic}")private String topic; // Full topic name, which needs to be concatenated.@Autowiredprivate RocketMQTemplate rocketMQTemplate;
org.springframework.messaging
.SendResult sendResult = rocketMQTemplate.syncSend(destination, message);/*------------------------------------------------------------------------*/rocketMQTemplate.syncSend(destination, MessageBuilder.withPayload(message).build())
/*** Description: Message producer*/@Servicepublic class SendMessage {// Use the full name of the topic, which can be either customized or concatenated in the format of "full namespace name%topic name".@Value("${rocketmq.namespace}%${rocketmq.producer1.topic}")private String topic;@Autowiredprivate RocketMQTemplate rocketMQTemplate;/*** Sync sending** @param message Message content* @param tags Subscribed tags*/public void syncSend(String message, String tags) {// 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;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);}}
@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 MessageConsumer implements RocketMQListener<String> {@Overridepublic void onMessage(String message) {System.out.println("Tag1Consumer receive message:" + message);}}
Was this page helpful?