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.
By default, messages in Message Queue for Apache RocketMQ are stored for three days. We recommend that you retain the default setting. 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 under a topic from 15:09:48 on June 7, 2019.
The following table compares the query methods.
|By message ID||Topic and message ID||Exact match||You can specify a topic and a message ID to find an exactly matched message and obtain its attributes.|
|By message key||Topic and message key||Fuzzy search||You can specify a topic and a message key, and then obtain the latest 64 messages
that contain the specified key under the topic.
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, the query may fail to return some messages.
|By topic||Topic and time range||Range match||You can specify a topic and a time range to obtain all messages that meet the conditions. This type of query returns a large number of messages, which makes locating the message you want more difficult.|
Recommended process for choosing a query method
We recommend that you choose a query method based on the following process.
- Log on to the Message Queue for Apache RocketMQ console.
- In the left-side navigation pane, click Instances.
- In the top navigation bar, select a region, such as China (Hangzhou).
- On the Instances page, find your instance, click More in the Actions column, and then select Message Query from the drop-down list.
- On the Message Query page, select a query method, set the required parameters, and click Search.
- Query by Message ID
When you query messages by message ID, you can specify a topic and a message ID to obtain the message that you want through exact match. We recommend that you print the message ID to the log when a message is sent. This step is beneficial to you when you want to troubleshoot issues with messages.
The SDK for Java is used in the following example to show how to obtain the message ID:
SendResult sendResult = producer.send(msg); String msgId = sendResult.getMessageId();
For how to obtain the message ID by using SDKs for other languages, see Overview.
- Query by Message Key
When you query messages by message key, Message Queue for Apache RocketMQ creates an index for messages based on the message key that you specify, and then Message Queue for Apache RocketMQ returns the messages that meet the condition.Notice
When you query messages by message key, take note of the following items:
- Ensure that a key is set for the message to be sent.
- Only the latest 64 messages that contain the specified key are returned. Therefore, we recommend that you set the key for each message to a unique and identifiable value.
The following sample code provides an example on how to specify a message key:
Message msg = new Message("Topic","*","Hello MQ".getBytes()); /** * Specify a key for each message to indicate the key business attribute of the message. We recommend that you specify a unique key for each message. * If the message is not received as expected, you can query the message in the Message Queue for Apache RocketMQ console by message key. Messages can be sent and received even if you do not specify the message key. */ msg.setKey("TestKey"+System.currentTimeMillis());
- Query by Topic
You can query messages by topic when both message ID and message key are unavailable. You can specify a topic and the time range during which the message is sent, obtain all messages that meet the conditions, and then find the message that you want.Notice
When you query messages by topic, take note of the following items:
- Obtain all messages that are sent in the specified time range under the topic. We recommend that you narrow down the time range to make it easier to find the message you want.
- A large number of messages are returned on multiple pages.
- Query by Message ID
Verify message consumption
Message Queue for Apache RocketMQ provides the message consumption verification feature. You can push a message to an online client, and check whether the message is consumed as expected.