Integrating Apache RocketMQ with Spring Boot: Features, Configuration, Code Samples, and Interview Essentials

This article provides a comprehensive guide to using Apache RocketMQ with Spring Boot, covering core components, basic concepts, key features, step‑by‑step configuration, producer and consumer code examples, advanced message types, transaction handling, and common interview questions for distributed messaging systems.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Integrating Apache RocketMQ with Spring Boot: Features, Configuration, Code Samples, and Interview Essentials

Apache RocketMQ is a high‑performance, high‑throughput distributed messaging middleware open‑sourced by Alibaba. It consists of Producer, Broker, and Consumer, where Producers send messages, Consumers receive them, and Brokers store and forward messages across multiple topics and partitions.

Main capabilities include business decoupling via publish/subscribe, traffic shaping, massive message accumulation, high availability with multi‑master/slave replication, reliable delivery through synchronous, asynchronous and one‑way sending, distributed transaction support, tag‑based filtering, FIFO ordered messages, and delayed messages.

Core components are Namesrv, a lightweight routing service that provides topic routing information, and Broker, which stores and forwards messages in master/slave groups.

Basic concepts such as Topic, Producer Group, Consumer Group, normal and strictly ordered messages, Message ID, and Tag are explained to help understand RocketMQ’s data model.

Key features listed include synchronous/asynchronous/one‑way sending, ordered, batch, transactional, delayed messages, tag/SQL92 filtering, message tracing, authentication/authorization, request‑reply pattern, and push/pull consumption.

Code demonstration

1. Add the starter dependency in

<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-spring-boot-starter</artifactId>
    <version>2.0.3</version>
</dependency>

2. Configure application.yaml with name‑server, consumer topic/group, and producer group.

rocketmq:
  name-server: localhost:9876
  consumer:
    topic: maker-order-topic
    group: my-group1
  producer:
    group: p-my-group1

3. Producer example (Spring MVC endpoint) sends an OrderModel object to the topic using rocketMQTemplate.syncSend.

@Resource
private RocketMQTemplate rocketMQTemplate;
private static String makerOrderTopic = "maker-order-topic";

@GetMapping("/send_make_order_message")
public Object send_make_order_message() {
    try {
        Long orderId = Long.valueOf(new Random().nextInt(1000000));
        OrderModel orderModel = OrderModel.builder()
            .orderId(orderId)
            .buyerUid(200000L)
            .amount(26.8)
            .shippingAddress("上海")
            .build();
        SendResult sendResult = rocketMQTemplate.syncSend(makerOrderTopic, orderModel);
        System.out.printf("Send message to topic %s , sendResult=%s %n", makerOrderTopic, sendResult);
        return "消息发送成功";
    } catch (Exception e) {
        e.printStackTrace();
        return "消息发送失败";
    }
}

4. Consumer example implements RocketMQListener<OrderModel> and prints the received order.

@Service
@RocketMQMessageListener(nameServer = "${rocketmq.name-server}",
    topic = "${rocketmq.consumer.topic}",
    consumerGroup = "${rocketmq.consumer.group}")
public class OrderConsumer implements RocketMQListener<OrderModel> {
    @Override
    public void onMessage(OrderModel orderModel) {
        System.out.printf("consumer received message: %s 
", JSON.toJSONString(orderModel));
    }
}

5. Various sending modes are illustrated: synchronous, asynchronous with SendCallback, ordered messages using a sharding key, delayed messages with delay levels, and transactional messages with a custom RocketMQLocalTransactionListener.

6. Transactional message flow includes half‑message sending, local transaction execution, commit/rollback decision, and broker‑side message check‑back when acknowledgments are lost.

Interview focus points cover ordered message guarantees, queue selection, message filtering strategies, deduplication via MessageId, transaction mechanics, producer reliability (synchronous send, transaction send), broker persistence (CommitLog, sync/async flush, master‑slave replication), and consumer offset management.

All source code and a demo project are available at https://github.com/aalansehaiyang/spring-boot-bulking (module spring-boot-bulking-rocketmq).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Distributed SystemsjavaSpring BootMessage QueueRocketMQTransactional Messaging
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.