edit-icon download-icon

Query Topic's consumer offset

Last Updated: Jun 21, 2018

This topic explains the OnsTopicStatus interface, which queries the current message status of a specified Topic.


This interface queries the total number of messages in the current Topic, and the last update time. It can gauge the usage rate of Topic resources. The TopicStatus interface returns the number of all messages in the specified Topic on the current server, and the last message writing time.

Request parameters

Name Type Required Description
OnsRegionId String Yes Set the region to be queried; its value is obtained through the OnsRegionList interface
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 The name of the queried Topic.

Response parameters

Name Type Description
RequestId String A public parameter unique for each request
HelpUrl String A help link
Data OnsTopicStatusResponse.Data The information storage data structure of the queried Topic.

OnsTopicStatusResponse.Data data structure

Member Type Description
totalCount Long The total number of the messages in all partitions of the current Topic.
lastTimeStamp Long The last update time of the current Topic.
  • OnsTopicCreate: creates a Topic
  • OnsConsumerStatus: queries the consumer status of the Consumer ID


As a reference, this example shows the process of accessing from the end point China East, and querying the number of messages in the Topic “JODIE_TEST_27dd” of the current user.

  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. OnsTopicStatusRequest request = new OnsTopicStatusRequest();
  19. request.setAcceptFormat(FormatType.JSON);
  20. /**
  21. *OnsRegionId refers to the resource of the region of MQ required to be accessed by API.
  22. *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.
  23. */
  24. request.setOnsRegionId("daily");
  25. request.setPreventCache(System.currentTimeMillis());
  26. request.setTopic("JODIE_TEST_27dd");
  27. try {
  28. OnsTopicStatusResponse response = iAcsClient.getAcsResponse(request);
  29. OnsTopicStatusResponse.Data data=response.getData();
  30. Long totalCount =data.getTotalCount();
  31. Long lastTimeStamp =data.getLastTimeStamp();
  32. System.out.println(response.getRequestId());
  33. } catch (ClientException e) {
  34. e.printStackTrace();
  35. }
  36. catch (Exception e) {
  37. e.printStackTrace();
  38. }
  39. }
Thank you! We've received your feedback.