tencent cloud

Feedback

Sending and Receiving General Messages

Last updated: 2023-10-19 11:04:14

    Overview

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

    Prerequisites

    You have created or prepared the required resources as instructed in Resource Creation and Preparation.
    You have installed JDK 1.8 or later.
    You have installed Maven 2.5 or later.
    You have downloaded the demo or obtained the demo in TencentCloud/rocketmq-demo in GitHub.

    Directions

    Step 1. Add dependencies

    Add dependencies to the pom.xml file.
    <dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-spring-boot-starter</artifactId>
    <version>2.2.2</version>
    </dependency>

    Step 2. Prepare configurations

    Add configuration information to the configuration file.
    server:
    port: 8082
    
    # RocketMQ configuration information
    rocketmq:
    # Service access address of TDMQ for RocketMQ
    name-server: rocketmq-xxx.rocketmq.ap-bj.public.tencenttdmq.com:9876
    # Producer configurations
    producer:
    # Producer group name
    group: group111
    # Role token
    access-key: eyJrZXlJZC....
    # Name of the authorized role
    secret-key: admin
    # Common configurations for the consumer
    consumer:
    # Role token
    access-key: eyJrZXlJZC....
    # Name of the authorized role
    secret-key: admin
    
    # Custom configurations based on business needs
    namespace: rocketmq-xxx|namespace1
    producer1:
    topic: testdev1
    consumer1:
    group: group111
    topic: testdev1
    subExpression: TAG1
    consumer2:
    group: group222
    topic: testdev1
    subExpression: 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
    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.
    
    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.

    Step 3. Send messages

    1. Inject 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.
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    2. Send messages. The message body can be a custom object or a message object that 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
    */
    @Service
    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".
    @Value("${rocketmq.namespace}%${rocketmq.producer1.topic}")
    private String topic;
    @Autowired
    private 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);
    }
    }
    Note
    Above is a sync sending sample. For more information on async sending and one-way sending, see the demo or TencentCloud/rocketmq-demo‌ in GitHub.

    Step 4. Consume messages

    @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> {
    
    @Override
    public void onMessage(String message) {
    System.out.println("Tag1Consumer receive message:" + message);
    }
    }
    You can configure multiple consumers as needed. The consumer configurations depend on your business requirements.
    Note
    For a complete sample, download the demo or (https://github.com/TencentCloud/rocketmq-demo/tree/main/java/rocketmq-demo/rocketmq4/src/main/java/com/tencent/demo/rocketmq4/simple!f2025fba6fb266a8503c27ebf173037b) obtain the demo in TencentCloud/rocketmq-demo‌ in GitHub.

    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 consumer group. Click Consumer 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