tencent cloud


Spring Boot Starter

Last updated: 2022-09-21 18:25:04


    This document describes how to use open-source SDK to send and receive messages by using the SDK for Spring Boot Starter as an example and helps you better understand the message sending and receiving processes.



    Step 1. Add dependencies

    Add dependencies to the pom.xml file.


    Step 2. Prepare configurations

    Add configuration information to the configuration file.

    port: 8082

    # RocketMQ configuration information
    # Service access address of TDMQ for RocketMQ
    name-server: rocketmq-xxx.rocketmq.ap-bj.public.tencenttdmq.com:9876
    # Producer configurations
    # Producer group name
    group: group111
    # Role token
    access-key: eyJrZXlJZC....
    # Name of the authorized role
    secret-key: admin
    # Common configurations for the consumer
    # Role token
    access-key: eyJrZXlJZC....
    # Name of the authorized role
    secret-key: admin

    # Custom configurations
    namespace: rocketmq-xxx|namespace1
    topic: testdev1
    group: group111
    topic: testdev1
    subExpression: TAG1
    group: group222
    topic: testdev1
    subExpression: TAG2
    Parameter Description
    name-server Cluster access address, which can be copied from Access Address in the Operation column on the Cluster page in the console. Namespace access addresses in new shared or exclusive clusters can be copied from the Namespace list.
    group Producer group name, which can be copied under the Group tab in the console.
    secret-key Role name, which can be copied on the Role Management page.
    access-key Role token, which can be copied in the Token column on the Role Management page. img
    namespace Namespace name, which can be copied under the Namespace tab in the console.
    topic Topic name, which can be copied under the Topic tab in the console.
    subExpression A parameter used to set the message tag.

    Step 3. Send messages

    1. Inject RcoketMQTemplate into the class that needs to send messages.
      private String topic; // Topic name, which needs to be concatenated.

      private RocketMQTemplate rocketMQTemplate;
    2. Send messages. The message body can be a custom object or a message object and is contained in the package org.springframework.messaging.
      SendResult sendResult = rocketMQTemplate.syncSend(destination, message);
      rocketMQTemplate.syncSend(destination, MessageBuilder.withPayload(message).build())
    3. Below is a complete sample.
      * Description: Message producer

      public 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".
      private String topic;
      private RocketMQTemplate rocketMQTemplate;
      * Sync sending
      * @param message Message content
      * @param tags Subscription 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,
      .setHeader(MessageConst.PROPERTY_KEYS, "yourKey") // Specify the business key
      System.out.printf("syncSend1 to topic %s sendResult=%s %n", topic, sendResult);

    Above is a sync sending sample. For more information on async sending and one-way sending, see the demo or RocketMQ Spring documentation.

    Step 4. Consume messages

    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> {

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

    You can configure multiple consumers as needed, and the consumer configurations depend on your business requirements.


    For the complete sample, see the demo or RocketMQ Spring.

    Step 5. View consumption details

    Log in to the TDMQ console, go to the Cluster > Group page, and view the list of clients connected to the group. Click View Details in the Operation column to view consumer details.

    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