edit-icon download-icon

Query message by Topic

Last Updated: Jan 30, 2018

OnsMessagePageQueryByTopic interface queries all messages within the specified time range by pages with transferred Topic and time range.

Usage

Querying a message by Topic is a range query. Query results are presented to the caller by pages. It is only applicable when no search criteria (MsgId or MsgKey) are available.

Note: It’s recommended to limit the query time range as much as possible when querying by Topic. Otherwise, the issue cannot be identified due to the large amount of matching messages.

Calling method

  • First, transfer the Topic, the start/end time, and page size, to query by pages. If there are messages, then it returns the messages on the first page, the total number of pages, and query Task ID by default.
  • Then retrieve the messages according to the query Task ID in the returned result. When retrieving the messages, transfer the Task ID and the current number of pages.

Request parameters

Name Type Required Description
OnsRegionId String Yes The region where the queried MQ is currently located. It can be obtained through the OnsRegionList method.
OnsPlatform String No The source of the request, which by default is POP platform.
PreventCache Long Yes It’s used for CSRF verification. Set it to the current system time.
Topic String Yes Topic of the message to be queried
BeginTime Long Yes The start time stamp of the query range
EndTime Long Yes The end time stamp of the query range
TaskId String No The query Task ID, not required for the first query, but required when retrieving messages later. It’s retrieved from the first return result.
CurrentPage Integer Yes The number of the page where the messages are to be retrieved, incrementing from 1. It cannot exceed the maximum page number when retrieving the messages.
PageSize Integer No The maximum amount of messages displayed on each page during query by pages, which by default is 20. The minimum value is 5, and the maximum value is 50.

Response parameters

Name Type Description
RequestId String A public parameter unique for each request
HelpUrl String A help link
MsgFoundDo MsgFoundDo Query result

MsgFoundDo data structure

Name Type Description
TaskId String Task ID returned when query is created for the first time; used for retrieving the message later.
MaxPageCount Long The maximum number of pages of the query result
CurrentPage Long The current page number
Data List(OnsRestMessageDo) A set of messages on the current page. Its type is consistent with the return result of messageGetById.
  • OnsMessageGetByMsgId: precisely queries a message by MsgId
  • OnsMessageGetByMsgKey: fuzzily queries a message by MsgKey

Example

Example of creating query

  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 ="ons.cn-hangzhou.aliyuncs.com";
  8. /**
  9. *Select Region based on the region you will access, and set the corresponding access point.
  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. /**
  20. *OnsRegionId refers to the resource of the region of MQ required to be accessed by API.
  21. *This value must be selected and configured by the list obtained through the OnsRegionList method because OnsRegionId is changing, and cannot be written as a fixed value.
  22. */
  23. request.setOnsRegionId("daily");
  24. request.setPreventCache(System.currentTimeMillis());
  25. request.setAcceptFormat(FormatType.JSON);
  26. request.setTopic("MingduanTest");
  27. request.setBeginTime(System.currentTimeMillis()-24*3600*1000);
  28. request.setEndTime(System.currentTimeMillis());
  29. request.setCurrentPage(1);
  30. request.setPageSize(20);
  31. try {
  32. OnsMessagePageQueryByTopicResponse response = iAcsClient.getAcsResponse(request);
  33. } catch (ServerException e) {
  34. e.printStackTrace();
  35. } catch (ClientException e) {
  36. e.printStackTrace();
  37. }
  38. }

Example of retrieving messages by pages

  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. /**
  9. *OnsRegionId refers to the resource of the region of MQ required to be accessed by API.
  10. *This value must be selected and configured by the list obtained through the OnsRegionList method because OnsRegionId is changing, and cannot be written as a fixed value.
  11. */
  12. request.setOnsRegionId("daily");
  13. request.setPreventCache(System.currentTimeMillis());
  14. request.setAcceptFormat(FormatType.JSON);
  15. request.setTopic("MingduanTest");
  16. request.setBeginTime(System.currentTimeMillis()-24*3600*1000);
  17. request.setEndTime(System.currentTimeMillis());
  18. request.setCurrentPage(3);
  19. request.setTaskId("0ADA91A600002A9F000002ADDA8137E8");
  20. try {
  21. OnsMessagePageQueryByTopicResponse response = iAcsClient.getAcsResponse(request);
  22. } catch (ServerException e) {
  23. e.printStackTrace();
  24. } catch (ClientException e) {
  25. e.printStackTrace();
  26. }
  27. }
Thank you! We've received your feedback.