If a message is not consumed as expected, you can query the message content for troubleshooting. Message Queue for Apache RocketMQ allows you to query messages by message ID, message key, or topic.
Messages are stored in Message Queue for Apache RocketMQ for three days by default. We recommend that you do not modify the default value. You can query only the messages that are produced within three days before the current query time. For example, if the current time is 15:09:48 on June 10, 2019, you can query messages that were produced in a topic from 15:09:48 on June 7, 2019.
The following table compares the three query methods.
|By message ID||Topic+Message ID||Exact match||You can specify a topic and message ID, and then locate a message and obtain its attributes by using exact match.|
|By message key||Topic+Message Key||Fuzzy match||You can specify a topic and message key, and then query the latest 64 messages that contain the specified key.
Notice We recommend that you set a unique key for each message to ensure that the number of messages with the same key does not exceed 64. Otherwise, some messages may be missed in the query.
|By topic||Topic+Time Range||Range match||You can specify a topic and time range, and then retrieve all messages that meet the condition in batches. This type of query allows you to query a large number of messages by using fuzzy match.|
Recommended query process
We recommend that you choose a query method based on the following process.
- Log on to the Message Queue for Apache RocketMQconsole.
- In the left-side navigation pane, click Instances.
- In the top navigation bar, select a region. In this example, select China (Hangzhou).
- On the Instances page, find the instance, click More in the Actions column, and then select Message Query from the drop-down list.
- On the Message Query page, click the tab based on your business requirements. On the tab that appears, set the parameters as prompted and click Search.
- Query by Message ID
If you query messages by message ID, exact match is used. You can specify a topic and a message ID, and then locate a message. Therefore, we recommend that you print the message ID in the log to facilitate troubleshooting after the message is sent.
In this example, RocketMQ SDK for Java is used. Run the following command to obtain the message ID:
SendResult sendResult = producer.send(msg); String msgId = sendResult.getMessageId();
For more information about SDKs for other languages, click Sample Code on the Groups page.
- Query by Message Key
Message Queue for Apache RocketMQ creates an index for messages based on the specified message key. When you enter the key to query messages, Message Queue for Apache RocketMQ returns the matched messages based on the index.Notice
If you query messages by message key, take note of the following items:
- The query condition is the specified message key.
- Only the latest 64 messages that contain the specified key are returned. Therefore, we recommend that you set a unique and business-distinctive key for each message.
Run the following command to set a message key:
Message msg = new Message("Topic","*","Hello MQ".getBytes()); /** * Set the key to be indexed for each message. The key value is the key attribute of the message. We recommend that you set a unique key for each message. * If you do not receive a message as expected, you can query the message in the Message Queue for Apache RocketMQ console. Messages can be sent and received as expected even if this attribute is not specified. */ msg.setKey("TestKey"+System.currentTimeMillis());
- Query by Topic
If you cannot query messages by message ID or message key, query messages by topic. You can specify a topic and time range for message sending, retrieve messages in batches, and then find the required data.Notice
If you query messages by topic, take note of the following items:
- Querying messages by topic is a process of range match to retrieve all messages that meet the time condition within the topic. The number of retrieved messages is large. Therefore, we recommend that you narrow down the time range.
- If you query messages by topic, a large number of messages are returned on multiple pages.
- Query by Message ID
You can view the returned messages on the Message Query page in the console. The displayed information of the messages includes the message ID, tag, key, sending time, and storage period. In addition, you can verify message consumption and download messages. For more information, see Query a message trace.
Verify message consumption
Message Queue for Apache RocketMQ provides the consumption verification feature. You can push a specified message to a specified online client to check whether the message is consumed as expected in terms of the logic and result.