Normal Message

Normal messages are messages that have no special features in Apache RocketMQ. They are different from featured messages such as fifo messages, delay messages, and transaction messages. This topic describes the scenarios, working mechanism, usage, and usage notes of normal messages.

Scenarios

Normal messages are generally used in microservice decoupling, data integration, and event-driven scenarios. Most of these scenarios have low or no requirements on timing or the sequence for processing messages other than reliable transmission channels.

Scenario 1: Asynchronous decoupling of microservices 在线消息处理

The preceding figure shows an online e-commerce transaction scenario. In this scenario, the upstream order system encapsulates order placement and payment as an independent normal message and sends the message to the Apache RocketMQ broker. Downstream systems then subscribe to the message from the broker on demand and process tasks based on the local consumption logic. Messages are independent of each other and do not need to be associated.

Scenario 2: Data integration transmission 数据传输

The preceding figure uses offline log collection as an example. An instrumentation component is used to collect operations logs from frontend applications and forward the logs to Apache RocketMQ. Each message is a piece of log data that requires no processing from Apache RocketMQ. Apache RocketMQ needs only to send the log data to the downstream storage and analysis systems. The backend applications are responsible for subsequent processing tasks.

Working mechanism

Definition of normal messages

Normal messages are messages with basic functions in Apache RocketMQ. Normal messages support asynchronous decoupling and communication between producers and consumers. 生命周期

Lifecycle of a normal message

  • Initialized: The message is built and initialized by the producer and is ready to be sent to a broker.

  • Ready: The message is sent to the broker, and is visible to the consumer and available for consumption.

  • Inflight: The message is obtained by the consumer and processed based on the local business logic of the consumer.

    In this process, the broker waits for the consumer to complete the consumption and submit the consumption result. If no response is received from the consumer in a certain period of time, Apache RocketMQ retries the message. For more information, see Consumption retry.

  • Acked: The consumer completes consumption and submits the consumption result to the broker. The broker marks whether the current message is successfully consumed.

    By default, Apache RocketMQ retains all messages. When the consumption result is submitted, the message data is logically marked as consumed instead of being deleted immediately. Therefore, the consumer can backtrack the message for re-consumption before it is deleted due to the expiration of the retention period or insufficient storage space.

  • Deleted: When the retention period of the message expires or the storage space is insufficient, Apache RocketMQ deletes the earliest saved message from the physical file in a rolling manner. For more information, see Message storage and cleanup.

Usage limits

Normal messages support only topics whose MessageType is Normal.

Example

Create topic

For creating topics in Apache RocketMQ 5.0, it is recommended to use the mqadmin tool. However, it is worth noting that message type needs to be added as a property parameter. Here is an example:

  1. sh mqadmin updateTopic -n <nameserver_address> -t <topic_name> -c <cluster_name> -a +message.type=NORMAL

Send messages

You can set index keys and filter tags to filter or search for normal messages. The following sample code shows how to send and receive normal messages in Java:

  1. // Send a normal message.
  2. MessageBuilder messageBuilder = new MessageBuilder();
  3. Message message = messageBuilder.setTopic("topic")
  4. // Specify the message index key so that you can accurately search for the message by using a keyword.
  5. .setKeys("messageKey")
  6. // Specify the message tag so that the consumer can filter the message based on the specified tag.
  7. .setTag("messageTag")
  8. // Message body.
  9. .setBody("messageBody".getBytes())
  10. .build();
  11. try {
  12. // Send the message. You need to pay attention to the sending result and capture exceptions such as failures.
  13. SendReceipt sendReceipt = producer.send(message);
  14. System.out.println(sendReceipt.getMessageId());
  15. } catch (ClientException e) {
  16. e.printStackTrace();
  17. }
  18. // Consumption example 1: When you consume a normal message as a push consumer, you need only to process the message in the message listener.
  19. MessageListener messageListener = new MessageListener() {
  20. @Override
  21. public ConsumeResult consume(MessageView messageView) {
  22. System.out.println(messageView);
  23. // Return the status based on the consumption result.
  24. return ConsumeResult.SUCCESS;
  25. }
  26. };
  27. // Consumption example 2: When you consume a normal message as a simple consumer, you must obtain and consume the message, and submit the consumption result.
  28. List<MessageView> messageViewList = null;
  29. try {
  30. messageViewList = simpleConsumer.receive(10, Duration.ofSeconds(30));
  31. messageViewList.forEach(messageView -> {
  32. System.out.println(messageView);
  33. // After consumption is complete, you must invoke ACK to submit the consumption result.
  34. try {
  35. simpleConsumer.ack(messageView);
  36. } catch (ClientException e) {
  37. e.printStackTrace();
  38. }
  39. });
  40. } catch (ClientException e) {
  41. // If the pull fails due to system traffic throttling or other reasons, you must re-initiate the request to obtain the message.
  42. e.printStackTrace();
  43. }

Usage notes

Set a globally unique index key to facilitate troubleshooting

You can set custom index keys, which are message keys, in Apache RocketMQ. When you query and trace messages, the index key can help you find these messages efficiently and accurately.

Therefore, when you send messages, we recommend that you use the unique information of the service, such as order ID and user ID, as an index. This helps you find messages quickly in the future.