All Products
Document Center


Last Updated: Mar 28, 2019


You can call this operation to query all messages in a topic in paging mode within a specified period of time through the input topic and time range.


Querying messages by topic is a range search. Query results are displayed on multiple pages to the caller. It is applicable only when no other search criteria (message ID and message key) are available. Note: You need to shorten the query time interval. Otherwise, problems will not be possible to locate because of excessive matching messages.

The following describes the procedure for calling this operation.

  1. Input the topic, start time, end time, and size of each page to query messages in paging mode. If messages are available, the messages on the first page, total number of pages, and query task ID are returned by default.

  2. Obtain messages based on the returned query task IDs. When you obtain messages, the query task IDs and current page number are input.

Request parameters

Name Type Required Description
PreventCache Long Yes The parameter used for CSRF verification, which can be set to the current system time. Unit: ms
InstanceId String No The ID of the instance.
Topic String Yes The topic of the message you want to query.
BeginTime Long Yes The start time of the query range.
EndTime Long Yes The end time of the query range.
TaskId String No The ID of the query task. It is not required for the first query but is required to obtain messages subsequently. It can be extracted based on the previously returned result.
CurrentPage Integer Yes The page number, increasing from 1. Maximum: 50. The value must not exceed the maximum.
PageSize Integer No The maximum number of messages displayed on each page in paging mode. Default value: 20. Minimum value: 5. Maximum value: 50.

Response parameters

Name Type Description
RequestId String A common parameter. Each request has a unique ID.
HelpUrl String A help link.
MsgFoundDo MsgFoundDo The query results.

Fields in MsgFoundDo

Name Type Description
TaskId String The ID returned for the first query task, which is used to obtain messages subsequently.
MaxPageCount Long The maximum number of pages of query results.
CurrentPage Long The current page number.
Data List(OnsRestMessageDo) A set of messages displayed on the current page, which is the same as the result returned for messageGetById.


Create a query task

  1. public static void main(String []args) {
  2. String regionId = "cn-hangzhou";
  3. String accessKey = "XXXXXXXXXXXXXXXXX";
  4. String secretKey = "XXXXXXXXXXXXXXXXX";
  5. String endPointName ="cn-hangzhou";
  6. String productName ="Ons";
  7. String domain ="";
  8. /**
  9. *Select a region and set an endpoint.
  10. */
  11. try {
  12. DefaultProfile.addEndpoint(endPointNameregionIdproductNamedomain);
  13. } catch (ClientException e) {
  14. e.printStackTrace();
  15. }
  16. IClientProfile profile= DefaultProfile.getProfile(regionIdaccessKeysecretKey);
  17. IAcsClient iAcsClient= new DefaultAcsClient(profile);
  18. OnsMessagePageQueryByTopicRequest request = new OnsMessagePageQueryByTopicRequest();
  19. request.setPreventCache(System.currentTimeMillis());
  20. request.setInstanceId("XXXXX");
  21. request.setTopic("MingduanTest");
  22. request.setBeginTime(System.currentTimeMillis()-24*3600*1000);
  23. request.setEndTime(System.currentTimeMillis());
  24. request.setCurrentPage(1);
  25. request.setPageSize(20);
  26. try {
  27. OnsMessagePageQueryByTopicResponse response = iAcsClient.getAcsResponse(request);
  28. } catch (ServerException e) {
  29. e.printStackTrace();
  30. } catch (ClientException e) {
  31. e.printStackTrace();
  32. }
  33. }

Obtain messages in paging mode

  1. public static void main(String []args) {
  2. String regionId = "cn-hangzhou";
  3. String accessKey = "XXXXX";
  4. String secretKey = "XXXXX";
  5. IClientProfile profile = DefaultProfile.getProfile(regionId accessKey secretKey);
  6. IAcsClient iAcsClient = new DefaultAcsClient(profile);
  7. OnsMessagePageQueryByTopicRequest request = new OnsMessagePageQueryByTopicRequest();
  8. request.setPreventCache(System.currentTimeMillis());
  9. request.setInstanceId("XXXXXXX");
  10. request.setTopic("MingduanTest");
  11. request.setBeginTime(System.currentTimeMillis()-24*3600*1000);
  12. request.setEndTime(System.currentTimeMillis());
  13. request.setCurrentPage(3);
  14. request.setTaskId("0ADA91A600002A9F000002ADDA8137E8");
  15. try {
  16. OnsMessagePageQueryByTopicResponse response = iAcsClient.getAcsResponse(request);
  17. } catch (ServerException e) {
  18. e.printStackTrace();
  19. } catch (ClientException e) {
  20. e.printStackTrace();
  21. }
  22. }