SoFunction
Updated on 2025-03-03

Using RocketMQ to implement batch message consumption function in SpringBoot

Preparation

Before you begin, make sure you have RocketMQ installed and configured. If it has not been installed yet, please refer to the RocketMQ official website for installation guide.

Project dependency

First, we need to add RocketMQ dependencies to our Spring Boot project. OpenFile, add the following content:

<dependency>
    <groupId></groupId>
    <artifactId>rocketmq-spring-boot-starter</artifactId>
    <version>2.1.0</version>
</dependency>

This dependency package contains everything you need to integrate with RocketMQ.

Configure RocketMQ

existAdd RocketMQ related configuration to the file:

rocketmq:
  name-server: 127.0.0.1:9876
  consumer:
    group: batchConsumerGroup
  producer:
    group: batchProducerGroup
  • name-server: The address of the RocketMQ service
  • : Grouping of message consumption
  • : Grouping of message production

make surename-serverThe address is correct and points to your RocketMQ service.

Production batch message

Create a message producer that sends batch messages. The following isSample code for:

import ;
import ;
import ;
import ;
import ;

import ;
import ;

@Service
public class BatchProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void sendBatchMessages() {
        List&lt;Message&lt;String&gt;&gt; messages = new ArrayList&lt;&gt;();
        for (int i = 0; i &lt; 10; i++) {
            Message&lt;String&gt; message = ("Hello RocketMQ " + i).build();
            (message);
        }
        
        ("BatchTopic", messages, 10000);
        ("Batch message sent successfully!");
    }
}
  • Here we create 10 messages and add them to the listmessagesmiddle.
  • CallMethod to send messages to topic in batchesBatchTopic

Consumption batch news

Next, we create a message consumer for bulk consumption of messages. The following isSample code for:

import ;
import ;
import ;

import ;

@Service
@RocketMQMessageListener(topic = "BatchTopic", consumerGroup = "batchConsumerGroup", selectorExpression = "*", consumeMessageBatchMaxSize = 10)
public class BatchConsumer implements RocketMQListener&lt;List&lt;String&gt;&gt; {

    @Override
    public void onMessage(List&lt;String&gt; messages) {
        ("Batch received message:");
        (message -&gt; ("Message Content:" + message));
    }
}

In this code:

  • @RocketMQMessageListenerAnnotation is used to identify this is a RocketMQ message listener, specifying the listening topic.BatchTopicGrouping and consumptionbatchConsumerGroup
  • consumeMessageBatchMaxSize = 10It means that each batch consumption is up to 10 messages.
  • onMessageThe method will process the received message list and print out the message content one by one.

Test batch message sending and consumption

Create a simple Spring Boot controller that triggers batch message sending. The following isCode:

import ;
import ;
import ;

@RestController
public class MessageController {

    @Autowired
    private BatchProducer batchProducer;

    @GetMapping("/sendBatchMessages")
    public String sendBatchMessages() {
        ();
        return "Batch Message Sent";
    }
}

By visithttp://localhost:8080/sendBatchMessagesTrigger message sending.

  • Calling this interface will send batch messages to the RocketMQ topicBatchTopic
  • BatchConsumerThese messages will be automatically received and batched.

Summarize

We successfully implemented RocketMQ batch message sending and consumption in Spring Boot:

  • Use the BatchProducer class to send messages in batches.
  • Use the BatchConsumer class to batch consume messages and set the maximum batch size.
  • Keep everything going smoothly with a simple REST API control message sending.

Batch message processing can improve the efficiency of message delivery and is suitable for high concurrency scenarios. This method can reduce network overhead and effectively utilize system resources.

This is the article about using RocketMQ to implement batch message consumption function in SpringBoot. For more related SpringBoot RocketMQ message consumption content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!