在分布式系统中,保持消息的顺序性是一个常见且重要的问题。RocketMQ 提供了一种有效的方式来确保消息的顺序消费。本文将通过代码示例,介绍如何使用 RocketMQ 实现消息的顺序生产和消费。
环境准备
在开始之前,请确保您已经配置好 RocketMQ 环境,并且在 MqConstant
类中定义了 RocketMQ 的 NameServer 地址。
顺序消息的生产
首先,我们需要编写生产者代码来发送顺序消息。我们会创建两个示例,一个简单的顺序生产示例,另一个则是基于业务逻辑(如订单流程)的顺序生产示例。
简单的顺序生产者
java
package com.takumilove.demo;
import com.takumilove.constant.MqConstant;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.junit.Test;
public class FOrderlyTest {
@Test
public void orderlyProducer() throws Exception {
DefaultMQProducer producer = new DefaultMQProducer("orderly-producer-group");
producer.setNamesrvAddr(MqConstant.NAME_SRV_ADDR);
producer.start();
for (int i = 0; i < 10; i++) {
Message message = new Message("orderlyTopic", ("我是第" + i + "个消息").getBytes());
producer.send(message);
}
producer.shutdown();
System.out.println("发送完毕:");
}
}
基于业务逻辑的顺序生产者
在这个示例中,我们假设有一个 Order
类表示订单,订单包含了 id
、orderNumber
、price
、date
和 status
等信息。
java
package com.takumilove.demo;
import com.takumilove.constant.MqConstant;
import com.takumilove.domain.Order;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MessageQueueSelector;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.junit.Test;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class FOrderlyTest {
@Test
public void orderlyProducer() throws Exception {
DefaultMQProducer producer = new DefaultMQProducer("orderly-producer-group");
producer.setNamesrvAddr(MqConstant.NAME_SRV_ADDR);
producer.start();
List<Order> orderList = Arrays.asList(
new Order(1, 111, 59D, new Date(), "下订单"),
new Order(2, 111, 59D, new Date(), "物流"),
new Order(3, 111, 59D, new Date(), "签收"),
new Order(4, 112, 89D, new Date(), "下订单"),
new Order(5, 112, 89D, new Date(), "物流"),
new Order(6, 112, 89D, new Date(), "拒收")
);
orderList.forEach(order -> {
Message message = new Message("orderlyTopic", order.toString().getBytes());
try {
producer.send(message, new MessageQueueSelector() {
@Override
public MessageQueue select(List<MessageQueue> list, Message message, Object o) {
int queueNumber = list.size();
Integer i = (Integer) o;
return list.get(i % queueNumber);
}
}, order.getOrderNumber());
} catch (Exception e) {
System.out.println("发送失败" + e.getMessage());
}
});
producer.shutdown();
System.out.println("发送完毕:");
}
}
顺序消息的消费
接下来,我们编写消费者代码来消费这些顺序消息。我们将分别展示简单顺序消费者和基于业务逻辑的顺序消费者。
简单的顺序消费者
java
package com.takumilove.demo;
import com.takumilove.constant.MqConstant;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.junit.Test;
import java.util.List;
public class FOrderlyTest {
@Test
public void orderlyConsumer() throws Exception {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("orderly-consumer-group");
consumer.setNamesrvAddr(MqConstant.NAME_SRV_ADDR);
consumer.subscribe("orderlyTopic", "*");
consumer.registerMessageListener(new MessageListenerConcurrently() {
@Override
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list,
ConsumeConcurrentlyContext consumeConcurrentlyContext) {
System.out.println(new String(list.get(0).getBody()));
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
}
});
consumer.start();
System.in.read();
}
}
基于业务逻辑的顺序消费者
java
package com.takumilove.demo;
import com.takumilove.constant.MqConstant;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.common.message.MessageExt;
import org.junit.Test;
import java.util.List;
public class FOrderlyTest {
@Test
public void orderlyConsumer() throws Exception {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("orderly-consumer-group");
consumer.setNamesrvAddr(MqConstant.NAME_SRV_ADDR);
consumer.subscribe("orderlyTopic", "*");
consumer.registerMessageListener(new MessageListenerOrderly() {
@Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list,
ConsumeOrderlyContext consumeOrderlyContext) {
MessageExt messageExt = list.get(0);
System.out.println(new String(messageExt.getBody()));
return ConsumeOrderlyStatus.SUCCESS;
}
});
consumer.start();
System.in.read();
}
}
总结
通过以上示例,我们展示了如何使用 RocketMQ 实现消息的顺序生产和消费。无论是简单的消息还是基于业务逻辑的消息,都可以通过 RocketMQ 提供的顺序消费机制来保证消息的有序性。这对于订单系统等需要严格顺序的场景尤为重要。