Ordered Message Sending

Ordered Message Introduction

Ordered messages have strict requirements on the order in which they are sent and consumed.

For a given Topic, messages are published and consumed strictly on a first-in-first-out (FIFO) basis, i.e., messages published first will be consumed first. Furthermore, as shown in the following figure, partitioned ordered messages are supported in Apache RocketMQ. The messages can be partitioned according to a certain criterion (e.g., ShardingKey). Messages with the same ShardingKey are assigned to the identical queue and consumed in order. 顺序消息发送

Ordered messages are also used in a wide range of application scenarios, such as the example of creating orders, the same order generation, payment, and shipment should be executed sequentially. In the case of simple messages, the messages of Order A may be polled and sent to different queues. The messages of different queues will not be able to maintain order. In contrast, ordered messages are sent by routing the sequence of messages with the same ShardingKey (same order number) to a logical queue.

Ordered Message Sample Code

The ordered message sample code is as follows:

  1. public class Producer {
  2. public static void main(String[] args) throws UnsupportedEncodingException {
  3. try {
  4. DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
  5. producer.start();
  6. String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", "TagE"};
  7. for (int i = 0; i < 100; i++) {
  8. int orderId = i % 10;
  9. Message msg =
  10. new Message("TopicTest", tags[i % tags.length], "KEY" + i,
  11. ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
  12. SendResult sendResult = producer.send(msg, new MessageQueueSelector() {
  13. @Override
  14. public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
  15. Integer id = (Integer) arg;
  16. int index = id % mqs.size();
  17. return mqs.get(index);
  18. }
  19. }, orderId);
  20. System.out.printf("%s%n", sendResult);
  21. }
  22. producer.shutdown();
  23. } catch (MQClientException | RemotingException | MQBrokerException | InterruptedException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. }

The difference here is mainly the call to the SendResult send(Message msg, MessageQueueSelector selector, Object arg) method, where MessageQueueSelector is the queue selector and arg is a Object in Java that can be passed in as a sorting criterion for sending partitioned messages.

Ordered Message Sending - 图2tip

MessageQueueSelector interface is as follows:

  1. public interface MessageQueueSelector {
  2. MessageQueue select(final List<MessageQueue> mqs, final Message msg, final Object arg);
  3. }

In the interface, mqs is the queue, msg is the message, and arg is the object passed in, the queue that message are sent to will be returned. In the above example, the orderId is used as the partitioning criterion, and the remainder of all queues is used to send messages with the same orderId to the same queue.

Consistency of Ordered Messages

If a Broker drops out, does the total number of queues change at that point?

If a change occurs, messages with the same ShardingKey will be sent to a different queue causing disorder. If no change occurs, messages will be sent to the queue of the offline Broker, which is bound to fail. Therefore, Apache RocketMQ provides two modes, to guarantee strict order over availability, create Topic by specifying the -o parameter (—order) to be true, which represents ordered messages:

  1. $ sh bin/mqadmin updateTopic -c DefaultCluster -t TopicTest -o true -n 127.0.0.1:9876
  2. create topic to 127.0.0.1:10911 success.
  3. TopicConfig [topicName=TopicTest, readQueueNums=8, writeQueueNums=8, perm=RW-, topicFilterType=SINGLE_TAG, topicSysFlag=0, order=true, attributes=null]

Secondly, make sure that the configuration orderMessageEnable and returnOrderTopicConfigToBroker in the NameServer must be true. If either of the above conditions is not met, availability is guaranteed rather than strict order.