In case of message consumption problems, you can query specific message content for troubleshooting. Message Queue for Apache RocketMQ provides you with three methods of querying messages: by message ID, by message key, and by topic.
Messages are stored in Message Queue for Apache RocketMQ for three days by default, and we recommend that you do not modify the value. That is, only the messages that are generated within three days before the current query time can be queried. For example, if the current time is 15:09:48 on June 10, 2019, messages that were generated under a topic as early as 15:09:48 on June 7, 2019 can be queried.
The following table compares the characteristics of the three methods.
|By message ID||Topic and message ID||Exact match query||By specifying a topic and message ID, you can locate a message precisely and retrieve its properties.|
|By message key||Topic and message Key||Fuzzy query||By specifying a topic and message key, you can query the latest 64 messages that contain
the specified key.
Notice We recommend that the message producer 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 and time range||Range query||By specifying a topic and time range, you can retrieve all messages that meet the conditions in batches. This type of query has a large query volume and implements only fuzzy matching.|
Recommended query process
We recommend that you query messages according to the following process.
- Log on to the Message Queue for Apache RocketMQ console. In the top navigation bar, select the region, such as China (Hangzhou).
- On the Instances page, find the target instance and click Details in the Actions column.
- In the left-side navigation pane, click Message Query.
- On the Message Query page, click any tab. On the tab that appears, enter related information and click
Search to query messages.
- By Message ID
Exact matching can be implemented if you query messages by message ID. You can obtain an exact message by specifying its topic and message ID. Therefore, we recommend that you print the message ID in the log after the message is sent, to facilitate exact match query and troubleshooting.
Take the Java SDK for example. You can retrieve the message ID as follows.
SendResult sendResult = producer.send(msg); String msgId = sendResult.getMessageId();
For more information about other languages, click Sample Code on the Group Management page.
- By Message Key
Message Queue for Apache RocketMQ creates index for messages according to the message key that you set. When you enter the key to query messages, Message Queue for Apache RocketMQ matches and returns the relevant messages according to the index.Notice
When you query by message key, pay attention to the following points:
- The query condition of a message key-based query is the message key property that you set.
- Only the latest 64 messages that meet the condition can be returned in a query by message key. Therefore, we recommend that you set a unique key for each message to distinguish between your businesses.
Set a message key as follows:
Message msg = new Message("Topic","*","Hello MQ".getBytes()); /** * Set the key to be retrieved for each message. The key value represents the key property of the message, which must be unique whenever possible. * If you fail to receive a message, you can query the message in the Message Queue for Apache RocketMQ console. Messages can be sent and received as usual even if this property is not set. */ msg.setKey("TestKey"+System.currentTimeMillis());
- By Topic
If you cannot retrieve any data by message ID or message key, query messages by topic. You can specify a topic and message sending time range to retrieve messages in batches and then locate the data that you are concerned about.Notice
When you query by topic, pay attention to the following:
- Query by topic is a range query for retrieving all messages that meet the time condition under the topic. Therefore, a large number of messages are retrieved. We recommend that you minimize the time interval to query.
- Query by topic involves a large amount of data, which is usually displayed on multiple pages.
- By Message ID
You can view the returned messages on the Message Query page in the console. The displayed information includes the message ID, tag, key, storage time point, and message details. You can also download message content, Query a message trace and click the group ID to implement Consumption verification.
Message Queue for Apache RocketMQ provides a consumption verification function that can push a specified message to a designated online client to detect whether the logic and result of message consumption by the client are as expected.