All Products
Search
Document Center

ApsaraMQ for RocketMQ:Query messages

Last Updated:Feb 28, 2024

ApsaraMQ for RocketMQ allows you to query messages by message ID, message key, and topic. You can use the message query feature to check whether messages are sent, verify whether message content is correct, and view message trace data.

Query methods

By default, messages are stored in ApsaraMQ for RocketMQ for three days. You can query only 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 from 15:09:48 on June 7, 2019 in a topic. We recommend that you do not change the default message storage period.

The following table compares the query methods.

Method

Condition

Type

Description

By message ID

Topic + Message ID

Exact match

You can specify a topic and a message ID to find the exactly matched message and obtain the corresponding attributes.

By message key

Topic + Message key

Fuzzy match

You can specify a topic and a message key and then obtain the most recent 64 messages that contain the specified key in the topic.

Important

We recommend that you specify a unique key for each message to ensure that the number of messages that have the same key does not exceed 64. Otherwise, specific messages may fail to be returned in a query.

By topic

Topic + Time range

Range match

You can specify a topic name and a time range to obtain all messages that meet the conditions. This type of query returns a large number of messages, which makes it difficult to locate the message that you want to query.

Recommended process for selecting a query method

We recommend that you select a query method based on the following process.

消息查询

Procedure

  1. Log on to the ApsaraMQ for RocketMQ console. In the left-side navigation pane, click Instances.

  2. In the top navigation bar, select a region, such as China (Hangzhou).

  3. On the Instances page, find the instance that you want to manage, click More in the Actions column, and then select Message Query from the drop-down list.

  4. On the Message Query page, select a query method and follow the on-screen instructions to configure the parameters. Then, click Search.

    • Query by Message ID

      When you query messages by message ID, exact match is used. You can specify the topic and ID of a message to obtain the message that you want. We recommend that you print the message ID to the log when a message is sent. This helps you troubleshoot issues.

      In the following example, the SDK for Java is used to show how to obtain a message ID:

         SendResult sendResult = producer.send(msg);
         String msgId = sendResult.getMessageId();

      For information about how to obtain message IDs by using SDKs for other programming languages, see Overview.

    • Query by Message Key

      When you query messages by message key, ApsaraMQ for RocketMQ creates an index for messages based on the message key that you specify and then returns the messages that meet the condition.

      Important
      • Make sure that a key is specified for the message that you want to query before the message is sent.

      • Only the most recent 64 messages that contain the specified key are returned. Therefore, we recommend that you specify a unique and identifiable key for each message.

      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 that is used for message query for each message. The key is the primary business attribute of a message. We recommend that you specify a globally unique key for each message. 
             * If a message is not received as expected, you can query the message in the ApsaraMQ for RocketMQ console by using the message key. A message can be sent and received even if you do not specify a key for the message. 
             */
            msg.setKey("TestKey"+System.currentTimeMillis());                                
    • Query by Topic

      If the ID and key of a message are unavailable, you can query the message by topic. You can specify a topic and a time range during which messages are sent to obtain all messages that meet the conditions. Then, you can obtain the message that you want to query.

      Important

      When you query messages by topic, take note of the following items:

      • This query method allows you to obtain all messages that are sent in a topic in a specific time range. We recommend that you narrow down the time range to locate the message that you want to query in an efficient manner.

      • A large number of messages are returned on multiple pages.

Query results

You can view the queried messages on the Message Query page in the ApsaraMQ for RocketMQ console. The displayed information about the messages includes the message IDs, tags, keys, sending time, and storage period. You can also query message traces, verify message consumption, and download messages in the ApsaraMQ for RocketMQ console. For information about how to query message traces, see Query message traces.

Verify message consumption

ApsaraMQ for RocketMQ provides the message consumption verification feature. You can push messages to a connected client and check whether the messages are consumed as expected.

The consumption verification feature is used only to check whether a client can consume messages by using the expected logic. The feature does not affect message receiving. Therefore, information such as message consumption status does not change after message consumption is verified.

Important

The consumption verification feature is used only to check whether a client can consume messages by using the expected logic. The feature does not affect message receiving. Therefore, information such as message consumption status does not change after message consumption is verified.

consumer_vaildate

References

You can view the trace, consumption status, and consumption result of a message based on the query result of the message. For more information, see Query message traces.